Want to shared some of my experience to use DataLink form to edit database connection string in windows form application.
The hardest part is to set DataLink form as child window of current form. But fortunately, I find a way to set HWnd property to get it solved.
Here is the code:
1: Public Function GetConnection() As String2: Dim dc As MSDASC.DataLinks = New MSDASC.DataLinksClass()3: Dim oConnection As Object = GetFakeOledbConnectionString(existingConnectionString)4: dc.hWnd = Me.Handle '//Set parent window to current form5: If dc.PromptEdit(oConnection) Then6: Return CType(oConnection, ADODB._Connection).ConnectionString7: Else8: Return Nothing9: End If10: End Function11:12: Private Function GetFakeOledbConnectionString(ByVal str As String) As Object13: If String.IsNullOrEmpty(str) Then14: str = "Integrated Security=SSPI; Persist Security Info=True;Password=1;"15: End If16: If Not str.StartsWith("Provider", StringComparison.OrdinalIgnoreCase) Then17: str = "Provider=SQLOLEDB;" + str18: str = str.Replace("Integrated Security=True", "Integrated Security=SSPI")19: If Not str.ToLower().Contains("Persist Security Info".ToLower()) Then20: str = str.TrimEnd(" ").TrimEnd(";") + "; Persist Security Info=True;"21: End If22: End If23: Dim position As Integer = str.IndexOf("AttachDBFile", StringComparison.OrdinalIgnoreCase)24: If position > 0 Then25: str.Replace("AttachDbFilename=;", "")26: End If27: Dim adoConnection As ADODB._Connection = New ADODB.ConnectionClass()28: adoConnection.ConnectionString = str29: Return adoConnectionEnd30: Function
No comments:
Post a Comment