Author: water and smoke
How to: Visually configure the data source connection string (1)
File adoconnectiondialog. VB
Option Strict Off
Namespace udata Namespace Udata. connectionui
Public Class adoconnectiondialog Class Adoconnectiondialog
Private Gconnectionstring As String = Nothing
Public Property connectionstring () Property Connectionstring () As String
Get
Return Gconnectionstring
End Get
Set ( Byval Value As String )
Gconnectionstring = Value
End Set
End Property
Public Function showdialog () FunctionShowdialog ()AsSystem. Windows. Forms. dialogresult
ReturnShowdialog (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
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 ' Create a string
Adoconnection = Datalinks. promptnew ()
Mresult = IIF ( Not Adoconnection Is Nothing , System. Windows. Forms. dialogresult. OK, system. Windows. Forms. dialogresult. Cancel)
Else ' Edit Existing string
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
The preceding class visually generates or edits an adoconnection data source connection string. Test the function. Code As follows (note the namespace when using IT ): Dim T As New Lzmtw. udata. connectionui. adoconnectiondialog
Private Sub button2_click () 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
The following processing key is how to convert the commonly used oleonstring of oledbconnection, odbcconnection, and sqlconnection to the connectionstring of adoconnection. It seems that this is not simple. It seems that you have to master some adoconnection-related features.
It has to be segmented. In the next section, the connection string characteristics of adoconnection