public Myconn As New SqlConnection(MyconnData.GetSQLConnectionString)
Imports System Imports System.IO PublicClass ConnDataClass ConnData Private m_strFileName AsString Private m_strConnectionText AsString Private m_strPesistSecurity AsString Private m_strIntegradedSecurity AsString Private m_strReconstructed AsString Private m_strDefaultFilename AsString Private m_strDatasource AsString Private m_strPassword AsString Private m_strUserID AsString Private m_strCatalog AsString'Database PublicEvent CloseApplication() PublicSub New()SubNew() m_strDefaultFilename ="DataLink.udl" ReadFromFile() SplitConnectionString() End Sub PublicFunction GetSQLConnectionString()Function GetSQLConnectionString() AsString ReconstructSQLConnectionString() Return m_strReconstructed End Function PrivateSub ReconstructSQLConnectionString()Sub ReconstructSQLConnectionString() m_strReconstructed = m_strPesistSecurity &";" IfNotIsNothing(m_strIntegradedSecurity) Then m_strReconstructed &= m_strIntegradedSecurity &";" Else m_strReconstructed &= m_strPassword &";" IfNot m_strUserID.Length =0Then m_strReconstructed &= m_strUserID &";" EndIf EndIf m_strReconstructed &= m_strCatalog &";" m_strReconstructed &= m_strDatasource &";" m_strCatalog = m_strCatalog.Substring(16) m_strDatasource = m_strDatasource.Substring(12) IfNotIsNothing(m_strPassword) Then m_strPassword = m_strPassword.Substring(9) m_strUserID = m_strUserID.Substring(8) EndIf End Sub 格式化从*.udl文件中,读取的字符串#Region "格式化从*.udl文件中,读取的字符串" PrivateSub SplitConnectionString()Sub SplitConnectionString() Dim strArray AsString() =Nothing Dim strString AsString Dim strProvider AsString Dim strDRIVER AsString Dim strSERVER AsString Dim strUID AsString Dim strDATABASE AsString strArray = m_strConnectionText.Split(";") If strArray(0).IndexOf("[oledb]") =-1Then Dim nAnswer As DialogResult nAnswer = MessageBox.Show("This file cannot be read"& vbCr & _ "Open another file ?", "Read udl File", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Error) If nAnswer = DialogResult.Yes Then ReadFromFile() SplitConnectionString() Exit Sub Else Dim strDescription AsString="Connection Failed."& vbCr &"Closing Application" Err.Raise(vbObjectError +999, , strDescription) RaiseEvent CloseApplication() EndIf EndIf ForEach strString In strArray If strString.IndexOf("Provider") >-1Then strProvider = strString.Substring(strString.IndexOf("Provider")) EndIf If strString.IndexOf("Password") >-1Then m_strPassword = strString EndIf If strString.IndexOf("Data") >-1Then m_strDatasource = strString EndIf If strString.IndexOf("User") >-1Then m_strUserID = strString EndIf If strString.IndexOf("Initial") >-1Then m_strCatalog = strString EndIf If strString.IndexOf("Integrated") >-1Then m_strIntegradedSecurity = strString EndIf If strString.IndexOf("Persist") >-1Then m_strPesistSecurity = strString EndIf If strString.IndexOf("DRIVER") >-1Then strDRIVER = strString.Substring(strString.IndexOf("DRIVER")) EndIf If strString.IndexOf("SERVER") >-1Then strSERVER = strString EndIf If strString.IndexOf("UID") >-1Then strUID = strString EndIf If strString.IndexOf("DATABASE") >-1Then strDATABASE = strString strDATABASE = strDATABASE.Remove(strDATABASE.Length -3, 3) EndIf Next 'Check if the connection string connects to SQLServer or Oracle Server. If strProvider.IndexOf("MSDASQL") >-1Then If strDRIVER.IndexOf("SQL Server") >-1Then m_strDatasource ="Data Source"& strSERVER.Substring(6) If strUID.Length >0Then m_strUserID ="User ID"& strUID.Substring(3) EndIf m_strCatalog ="Initial Catalog "& strDATABASE.Substring(8) Else 'not sqlserver database Dim nAnswer As DialogResult nAnswer = MessageBox.Show("This file does not connect to SQL Server Database"& vbCr & _ "Open another file ?", "Read udl File", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Error) If nAnswer = DialogResult.Yes Then ReadFromFile() SplitConnectionString() Exit Sub Else Dim strDescription AsString="Connection Failed."& vbCr &"Closing Application" Err.Raise(vbObjectError +999, , strDescription) EndIf EndIf ElseIf strProvider.IndexOf("SQLOLEDB") >-1Then m_strDatasource = m_strDatasource.Remove(m_strDatasource.Length -2, 2) ElseIf strProvider.IndexOf("OraOLEDB") >-1Then'Oracle provider for OLEDB ElseIf strProvider.IndexOf("MSDAORA") >-1Then'Microsoft Provider for Oracle Else 'not Oracle or sqlserver database Connection Dim nAnswer As DialogResult nAnswer = MessageBox.Show("This file does not connect to Oracle Server or SQL Database"& vbCr & _ "Open another file ?", "Read udl File", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Error) If nAnswer = DialogResult.Yes Then ReadFromFile() SplitConnectionString() Exit Sub Else Dim strDescription AsString="Connection Failed."& vbCr &"Closing Application" Err.Raise(vbObjectError +999, , strDescription) EndIf EndIf End Sub #End Region 读取*.UDL文件内容,如是*.UDL文件不存在,则创建#Region "读取*.UDL文件内容,如是*.UDL文件不存在,则创建" PrivateSub ReadFromFile()Sub ReadFromFile() Dim myStreamReader As StreamReader Dim strFileName AsString Try m_strFileName = AppDomain.CurrentDomain.BaseDirectory & m_strDefaultFilename strFileName = m_strFileName 'If the UDL file cannot be found Create a new one IfNot File.Exists(m_strFileName) Then m_strFileName = strFileName CreateUDLFile(strFileName) EndIf myStreamReader = File.OpenText(m_strFileName) ' Read the entire file in one pass m_strConnectionText = myStreamReader.ReadToEnd() Catch exc As Exception ' Show the exception to the user. Dim strDescription AsString="File could not be opened or read."+ vbCrLf + _ "Please verify that the filename is correct, "+ _ "and that you have read permissions for the desired "+ _ "directory."+ vbCrLf + vbCrLf +"Exception: "+ exc.Message Err.Raise(vbObjectError +991, , strDescription) RaiseEvent CloseApplication() Finally ' Close the object if it has been created. IfNot myStreamReader IsNothingThen myStreamReader.Close() EndIf EndTry End Sub PrivateSub CreateUDLFile()Sub CreateUDLFile(ByVal strFileName AsString) 'Create an empty text file Dim fs AsNew System.IO.FileStream(strFileName, System.IO.FileMode.CreateNew) fs.Close() 'Call Data Link App and write information to it Dim prProcess AsNew ProcessStartInfo prProcess.FileName = strFileName Dim pProcess As Process = Process.Start(prProcess) pProcess.WaitForInputIdle() pProcess.WaitForExit() pProcess.Close() 'Check if the created file is a valid UDl file, if not, delete it. Dim myStreamReader As System.IO.StreamReader = System.IO.File.OpenText(strFileName) Dim m_strConnectionText AsString= myStreamReader.ReadToEnd() myStreamReader.Close() Dim strArray() AsString= m_strConnectionText.Split(";") If strArray(0).IndexOf("[oledb]") =-1Then If System.IO.File.Exists(strFileName) Then System.IO.File.Delete(strFileName) EndIf EndIf End Sub #End Region End Class