Author:水如烟
HOW TO:可视化配置数据源连接字符串(一)
文件AdoConnectionDialog.vb
上面的类可视化生成或编辑一个AdoConnection的数据源连接字符串.测试代码如下(各位使用时注意命名空间):
下面的处理关键是我们常用到的OleDbConnection、OdbcConnection、SqlConnection的ConnectionString与AdoConnection的ConnectionString如何相互转换。这好象并不简单,看来还得掌握一些AdoConnection的相关特性才行。
还得分节。下一节,AdoConnection的连接字符串有关特性
HOW TO:可视化配置数据源连接字符串(一)
文件AdoConnectionDialog.vb
Option Strict Off
Namespace uData.ConnectionUI
Public Class AdoConnectionDialog
Private gConnectionString As String = Nothing
Public Property ConnectionString() As String
Get
Return gConnectionString
End Get
Set(ByVal value As String)
gConnectionString = value
End Set
End Property
Public Function ShowDialog() As System.Windows.Forms.DialogResult
Return ShowDialog(Nothing)
End Function
Public 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
Dim AdoConnection As Object
Dim DataLinks As Object = CreateObject("DataLinks")
If owner Is Nothing Then
DataLinks.hWnd = IntPtr.Zero
Else
DataLinks.hWnd = owner.Handle
End If
If gConnectionString Is Nothing OrElse gConnectionString.Trim.Equals(String.Empty) Then '新建字符串
AdoConnection = DataLinks.PromptNew()
mResult = IIf(Not AdoConnection Is Nothing, System.Windows.Forms.DialogResult.OK, System.Windows.Forms.DialogResult.Cancel)
Else '编辑已有字符串
AdoConnection = CreateObject("ADODB.Connection")
AdoConnection.ConnectionString = gConnectionString
mResult = IIf(DataLinks.PromptEdit(AdoConnection), System.Windows.Forms.DialogResult.OK, System.Windows.Forms.DialogResult.Cancel)
End If
If mResult = System.Windows.Forms.DialogResult.OK Then
gConnectionString = AdoConnection.ConnectionString
End If
AdoConnection = Nothing
DataLinks = Nothing
Return mResult
End Function
End Class
End Namespace
Namespace uData.ConnectionUI
Public Class AdoConnectionDialog
Private gConnectionString As String = Nothing
Public Property ConnectionString() As String
Get
Return gConnectionString
End Get
Set(ByVal value As String)
gConnectionString = value
End Set
End Property
Public Function ShowDialog() As System.Windows.Forms.DialogResult
Return ShowDialog(Nothing)
End Function
Public 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
Dim AdoConnection As Object
Dim DataLinks As Object = CreateObject("DataLinks")
If owner Is Nothing Then
DataLinks.hWnd = IntPtr.Zero
Else
DataLinks.hWnd = owner.Handle
End If
If gConnectionString Is Nothing OrElse gConnectionString.Trim.Equals(String.Empty) Then '新建字符串
AdoConnection = DataLinks.PromptNew()
mResult = IIf(Not AdoConnection Is Nothing, System.Windows.Forms.DialogResult.OK, System.Windows.Forms.DialogResult.Cancel)
Else '编辑已有字符串
AdoConnection = CreateObject("ADODB.Connection")
AdoConnection.ConnectionString = gConnectionString
mResult = IIf(DataLinks.PromptEdit(AdoConnection), System.Windows.Forms.DialogResult.OK, System.Windows.Forms.DialogResult.Cancel)
End If
If mResult = System.Windows.Forms.DialogResult.OK Then
gConnectionString = AdoConnection.ConnectionString
End If
AdoConnection = Nothing
DataLinks = Nothing
Return mResult
End Function
End Class
End Namespace
上面的类可视化生成或编辑一个AdoConnection的数据源连接字符串.测试代码如下(各位使用时注意命名空间):
Dim t As New LzmTW.uData.ConnectionUI.AdoConnectionDialog
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If t.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
Console.WriteLine(t.ConnectionString)
Else
Console.WriteLine("Cancel")
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If t.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
Console.WriteLine(t.ConnectionString)
Else
Console.WriteLine("Cancel")
End If
End Sub
下面的处理关键是我们常用到的OleDbConnection、OdbcConnection、SqlConnection的ConnectionString与AdoConnection的ConnectionString如何相互转换。这好象并不简单,看来还得掌握一些AdoConnection的相关特性才行。
还得分节。下一节,AdoConnection的连接字符串有关特性