分享
 
 
 

HOW TO:可视化配置数据源连接字符串(三)

王朝other·作者佚名  2006-05-06
窄屏简体版  字體: |||超大  

Author:水如烟

HOW TO:可视化配置数据源连接字符串(一) HOW TO:可视化配置数据源连接字符串(二)

这一节原计划说AdoConnection连接字符串有关特性的,在这省了,以下只贴代码了.有些匆忙.仅供参考.

项目除了上面提到的AdoConnectionDialog.vb还有以下四个文件:

DbConnectionType.vb

Namespace uDataNamespace uData.ConnectionUI

Friend Enum DbConnectionTypeEnum DbConnectionType

OleDb

Odbc

Sql

Other

End Enum

End Namespace

ConvertType.vb

Namespace uDataNamespace uData.ConnectionUI

Friend Enum ConvertTypeEnum ConvertType

ADO2NET

NET2ADO

End Enum

End Namespace

ConnectionStringConver.vb

Option Strict Off

Namespace uDataNamespace uData.ConnectionUI

Friend Class ConnectionStringConverClass ConnectionStringConver

Private Sub New()Sub New()

End Sub

Private Shared Function AdoToOleDb()Function AdoToOleDb(ByVal adoConnectionString As String) As String

Dim mBuilder As New OleDb.OleDbConnectionStringBuilder(adoConnectionString)

Return mBuilder.ConnectionString

End Function

Private Shared Function AdoToOdbc()Function AdoToOdbc(ByVal adoConnectionString As String) As String

Dim mResult As New Text.StringBuilder

Dim AdoConnection As Object

AdoConnection = CreateObject("ADODB.Connection")

AdoConnection.ConnectionString = adoConnectionString

Dim mDataSource As String = Nothing

Dim mExtendedProperties As String = Nothing

Dim Key As String = Nothing

Dim Value As String = Nothing

For Each p As Object In AdoConnection.Properties

Key = p.Name.ToString

Value = p.Value

If Key <> "Provider" Then

If Key = "Data Source" Then

mDataSource = Value

ElseIf Key = "Extended Properties" Then

mExtendedProperties = Value

Else

If Not Value Is Nothing Then mResult.AppendFormat("{0}={1};", Key, Value)

End If

End If

Next

If mDataSource = "" Then

mResult.Append(mExtendedProperties)

Else

mResult.AppendFormat("Dsn={0};", mDataSource)

End If

AdoConnection = Nothing

Return mResult.ToString

End Function

Private Shared Function AdoToSql()Function AdoToSql(ByVal adoConnectionString As String) As String

adoConnectionString = System.Text.RegularExpressions.Regex.Replace(adoConnectionString, "Provider.*?;", "", Text.RegularExpressions.RegexOptions.IgnoreCase)

Dim mBuilder As New SqlClient.SqlConnectionStringBuilder(adoConnectionString)

Return mBuilder.ConnectionString

End Function

Private Shared Function OleDbToAdo()Function OleDbToAdo(ByVal oleDbConnectionString As String) As String

Dim mBuilder As New OleDb.OleDbConnectionStringBuilder(oleDbConnectionString)

If mBuilder.Provider = "" Then mBuilder.Provider = "SQLOLEDB.1;"

Return mBuilder.ConnectionString

End Function

Private Shared Function OdbcToAdo()Function OdbcToAdo(ByVal odbcConnectionString As String) As String

If odbcConnectionString Is Nothing Then odbcConnectionString = ""

odbcConnectionString = odbcConnectionString.ToLower

If odbcConnectionString.Contains("dsn=") Then

Dim AdoConnection As Object

AdoConnection = CreateObject("ADODB.Connection")

AdoConnection.ConnectionString = odbcConnectionString

Dim mExtendedProperties As String = AdoConnection.Properties("Extended Properties").Value

If mExtendedProperties.IndexOf(";") = -1 Then

odbcConnectionString = odbcConnectionString.ToLower.Replace("dsn", "Data Source")

End If

AdoConnection = Nothing

End If

Dim mBuilder As New Odbc.OdbcConnectionStringBuilder(odbcConnectionString)

Return mBuilder.ConnectionString

End Function

Private Shared Function SqlToAdo()Function SqlToAdo(ByVal sqlConnectionString As String) As String

Dim mBuilder As New SqlClient.SqlConnectionStringBuilder(sqlConnectionString)

sqlConnectionString = mBuilder.ConnectionString

sqlConnectionString = System.Text.RegularExpressions.Regex.Replace(sqlConnectionString, "integrated security.*?true\s{0,};{0,1}", "Integrated Security=SSPI;", Text.RegularExpressions.RegexOptions.IgnoreCase)

Return "Provider = SQLOLEDB.1;" & sqlConnectionString

End Function

Public Shared Function GetString()Function GetString(ByVal dbconnectiontype As DbConnectionType, ByVal convertype As ConvertType, ByVal cnString As String) As String

Dim mResult As String = Nothing

Select Case dbconnectiontype

Case ConnectionUI.DbConnectionType.OleDb

If convertype = ConvertType.NET2ADO Then

mResult = OleDbToAdo(cnString)

Else

mResult = AdoToOleDb(cnString)

End If

Case ConnectionUI.DbConnectionType.Odbc

If convertype = ConvertType.NET2ADO Then

mResult = OdbcToAdo(cnString)

Else

mResult = AdoToOdbc(cnString)

End If

Case ConnectionUI.DbConnectionType.Sql

If convertype = ConvertType.NET2ADO Then

mResult = SqlToAdo(cnString)

Else

mResult = AdoToSql(cnString)

End If

Case Else ' ConnectionUI.DbConnectionType.Other

Throw New NullReferenceException("不支持当前连接方式")

End Select

Return mResult

End Function

End Class

End Namespace

ConnectionDialog.vb

Namespace uDataNamespace uData.ConnectionUI

Public Class ConnectionDialogClass ConnectionDialog

Private gDbConnection As IDbConnection = Nothing

Private gConnectionString As String = Nothing

'连接方式

Private gDbConnectionType As DbConnectionType = DbConnectionType.Odbc

Private gAdoConnectionString As String = Nothing

Public Property DbConnection()Property DbConnection() As IDbConnection

Get

Return gDbConnection

End Get

Set(ByVal value As IDbConnection)

gDbConnection = value

End Set

End Property

Public Property ConnectionString()Property ConnectionString() As String

Get

Return gConnectionString

End Get

Set(ByVal value As String)

gConnectionString = value

End Set

End Property

'判断连接方式

Private Sub Check()Sub Check()

'优先判断DbConnection.若无,判断连接字符串;若字符串为空,则取默认连接方式为Odbc

If gDbConnection Is Nothing Then

If gConnectionString = "" Then

gDbConnectionType = DbConnectionType.Odbc

Else

'根据连接字符串的特征字来判断是哪类连接方式

If gConnectionString.ToLower.Contains("provider=") Then

gDbConnectionType = DbConnectionType.OleDb

ElseIf gConnectionString.ToLower.Contains("dsn=") Then

gDbConnectionType = DbConnectionType.Odbc

ElseIf gConnectionString.ToLower.Contains("driver=") Then

gDbConnectionType = DbConnectionType.Odbc

Else

gDbConnectionType = DbConnectionType.Sql

End If

End If

Else

If TypeOf gDbConnection Is OleDb.OleDbConnection Then

gDbConnectionType = DbConnectionType.OleDb

ElseIf TypeOf gDbConnection Is Odbc.OdbcConnection Then

gDbConnectionType = DbConnectionType.Odbc

ElseIf TypeOf gDbConnection Is SqlClient.SqlConnection Then

gDbConnectionType = DbConnectionType.Sql

Else

gDbConnectionType = DbConnectionType.Other

End If

'也取连接字符串

gConnectionString = gDbConnection.ConnectionString

End If

'初始化AdoConnectionString

gAdoConnectionString = Nothing

End Sub

Public Function ShowDialog()Function ShowDialog() As System.Windows.Forms.DialogResult

Return ShowDialog(Nothing)

End Function

Public Function ShowDialog()Function ShowDialog(ByVal owner As System.Windows.Forms.Form) As System.Windows.Forms.DialogResult

Dim mResult As System.Windows.Forms.DialogResult = System.Windows.Forms.DialogResult.OK

Check()

Dim mDialog As New AdoConnectionDialog

With mDialog

.ConnectionString = ConnectionStringConver.GetString(gDbConnectionType, ConvertType.NET2ADO, gConnectionString)

mResult = .ShowDialog(owner)

End With

If mResult = Windows.Forms.DialogResult.OK Then

gConnectionString = ConnectionStringConver.GetString(gDbConnectionType, ConvertType.ADO2NET, mDialog.ConnectionString)

If Not gDbConnection Is Nothing Then

'正处在连接状态下不可置值,这里强制关闭了不好,由用户处理好了

' If gDbConnection.State = ConnectionState.Open Then gDbConnection.Close()

gDbConnection.ConnectionString = gConnectionString

End If

End If

mDialog = Nothing

Return mResult

End Function

End Class

End Namespace

以下为测试项目用到的一个文件:

ConnectionDialogTest.vb

Public Class ConnectionDialogTestClass ConnectionDialogTest

Private OleDbCn As New OleDb.OleDbConnection

Private OdbcCn As New Odbc.OdbcConnection

Private SqlCn As New SqlClient.SqlConnection

Private gConnectionString As String

Public Property ConnectionString()Property ConnectionString() As String

Get

Return gConnectionString

End Get

Set(ByVal value As String)

gConnectionString = value

End Set

End Property

Private gNowCn As Common.DbConnection

Public ReadOnly Property DbConnection()Property DbConnection() As Common.DbConnection

Get

Return gNowCn

End Get

End Property

Private gDialog As New LzmTW.uData.ConnectionUI.ConnectionDialog

Public Function ShowDialog()Function ShowDialog(ByVal owner As Form, ByVal byConnectionString As Boolean, ByVal DbTypeOrConnectionstring As String) As DialogResult

Dim mResult As DialogResult = DialogResult.OK

If byConnectionString Then

gDialog.ConnectionString = DbTypeOrConnectionstring

gDialog.DbConnection = Nothing

mResult = gDialog.ShowDialog(owner)

If mResult = DialogResult.OK Then

gConnectionString = gDialog.ConnectionString

If gConnectionString.ToLower.Contains("provider=") Then

gNowCn = New OleDb.OleDbConnection

ElseIf gConnectionString.ToLower.Contains("dsn=") Then

gNowCn = New Odbc.OdbcConnection

ElseIf gConnectionString.ToLower.Contains("driver=") Then

gNowCn = New Odbc.OdbcConnection

Else

gNowCn = New SqlClient.SqlConnection

End If

gNowCn.ConnectionString = gConnectionString

End If

Else

Dim TmpCn As Common.DbConnection

If DbTypeOrConnectionstring = "OleDbConnection" Then

TmpCn = OleDbCn

ElseIf DbTypeOrConnectionstring = "OdbcConnection" Then

TmpCn = OdbcCn

Else

TmpCn = SqlCn

End If

gDialog.DbConnection = TmpCn

mResult = gDialog.ShowDialog(owner)

If mResult = DialogResult.OK Then

gNowCn = TmpCn

gConnectionString = gNowCn.ConnectionString

End If

End If

Return mResult

End Function

End Class

测试界面代码:

Public Class Form1Class Form1

Private Demo As New ConnectionDialogTest

Private Sub SetMessage()Sub SetMessage(ByVal text As String)

Me.Label1.Text = String.Format("Msg:{0}", text)

End Sub

Private Sub OpenTest_Click()Sub OpenTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenTest.Click

Demo.DbConnection.Open()

SetMessage("已打开")

End Sub

Private Sub CloseTest_Click()Sub CloseTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseTest.Click

Demo.DbConnection.Close()

SetMessage("已关闭")

End Sub

Private Sub DbConnectionEdit_Click()Sub DbConnectionEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DbConnectionEdit.Click

If Demo.ShowDialog(Me, False, Me.GetDbName) = Windows.Forms.DialogResult.OK Then

Me.RichTextBox1.AppendText(System.Environment.NewLine & Demo.ConnectionString)

End If

End Sub

Private Sub ConnectionStringEdit_Click()Sub ConnectionStringEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectionStringEdit.Click

If Demo.ShowDialog(Me, True, Me.RichTextBox1.SelectedText) = Windows.Forms.DialogResult.OK Then

Me.RichTextBox1.AppendText(System.Environment.NewLine & Demo.ConnectionString)

End If

End Sub

Private Function GetDbName()Function GetDbName() As String

Dim tmpRadio As RadioButton

If Me.RadioButton1.Checked Then

tmpRadio = Me.RadioButton1

ElseIf Me.RadioButton2.Checked Then

tmpRadio = Me.RadioButton2

Else

tmpRadio = Me.RadioButton3

End If

Return tmpRadio.Text

End Function

End Class

项目代码下载,不做异常处理

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有