调用VB.NET与*.udl文件数据库连接的步骤
以前用VB6时连接数据库时,连接字符串调用*.udl文件,觉得很方便.但在VB.NET里调用*.udl文件时,有很大不同,VB6简单的写上:file name=c:/*.dul就行了,但VB.net是通过读取文件的方式来处理的,下面是全部代码。与大家共享:在您用时只要调用这个类:
Dim MyconnData As New ConnData
public Myconn As New SqlConnection(MyconnData.GetSQLConnectionString)
ImportsSystem
ImportsSystem.IO



PublicClassConnDataClassConnData
Privatem_strFileNameAsString
Privatem_strConnectionTextAsString

Privatem_strPesistSecurityAsString
Privatem_strIntegradedSecurityAsString
Privatem_strReconstructedAsString

Privatem_strDefaultFilenameAsString
Privatem_strDatasourceAsString
Privatem_strPasswordAsString
Privatem_strUserIDAsString
Privatem_strCatalogAsString'Database
PublicEventCloseApplication()

PublicSubNew()SubNew()
m_strDefaultFilename="DataLink.udl"
ReadFromFile()
SplitConnectionString()
EndSub







PublicFunctionGetSQLConnectionString()FunctionGetSQLConnectionString()AsString
ReconstructSQLConnectionString()
Returnm_strReconstructed
EndFunction

PrivateSubReconstructSQLConnectionString()SubReconstructSQLConnectionString()

m_strReconstructed=m_strPesistSecurity&";"

IfNotIsNothing(m_strIntegradedSecurity)Then
m_strReconstructed&=m_strIntegradedSecurity&";"
Else
m_strReconstructed&=m_strPassword&";"
IfNotm_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

EndSub


格式化从*.udl文件中,读取的字符串#Region"格式化从*.udl文件中,读取的字符串"



PrivateSubSplitConnectionString()SubSplitConnectionString()
DimstrArrayAsString()=Nothing
DimstrStringAsString
DimstrProviderAsString

DimstrDRIVERAsString
DimstrSERVERAsString
DimstrUIDAsString
DimstrDATABASEAsString


strArray=m_strConnectionText.Split(";")

IfstrArray(0).IndexOf("[oledb]")=-1Then
DimnAnswerAsDialogResult
nAnswer=MessageBox.Show("Thisfilecannotberead"&vbCr&_
"Openanotherfile?","ReadudlFile",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Error)

IfnAnswer=DialogResult.YesThen
ReadFromFile()
SplitConnectionString()
ExitSub
Else
DimstrDescriptionAsString="ConnectionFailed."&vbCr&"ClosingApplication"
Err.Raise(vbObjectError+999,,strDescription)
RaiseEventCloseApplication()
EndIf
EndIf


ForEachstrStringInstrArray

IfstrString.IndexOf("Provider")>-1Then
strProvider=strString.Substring(strString.IndexOf("Provider"))
EndIf

IfstrString.IndexOf("Password")>-1Then
m_strPassword=strString
EndIf

IfstrString.IndexOf("Data")>-1Then
m_strDatasource=strString
EndIf

IfstrString.IndexOf("User")>-1Then
m_strUserID=strString
EndIf

IfstrString.IndexOf("Initial")>-1Then
m_strCatalog=strString
EndIf

IfstrString.IndexOf("Integrated")>-1Then
m_strIntegradedSecurity=strString
EndIf

IfstrString.IndexOf("Persist")>-1Then
m_strPesistSecurity=strString
EndIf
IfstrString.IndexOf("DRIVER")>-1Then
strDRIVER=strString.Substring(strString.IndexOf("DRIVER"))
EndIf
IfstrString.IndexOf("SERVER")>-1Then
strSERVER=strString
EndIf
IfstrString.IndexOf("UID")>-1Then
strUID=strString
EndIf
IfstrString.IndexOf("DATABASE")>-1Then
strDATABASE=strString
strDATABASE=strDATABASE.Remove(strDATABASE.Length-3,3)
EndIf

Next

'CheckiftheconnectionstringconnectstoSQLServerorOracleServer.
IfstrProvider.IndexOf("MSDASQL")>-1Then
IfstrDRIVER.IndexOf("SQLServer")>-1Then

m_strDatasource="DataSource"&strSERVER.Substring(6)
IfstrUID.Length>0Then
m_strUserID="UserID"&strUID.Substring(3)
EndIf
m_strCatalog="InitialCatalog"&strDATABASE.Substring(8)

Else
'notsqlserverdatabase
DimnAnswerAsDialogResult
nAnswer=MessageBox.Show("ThisfiledoesnotconnecttoSQLServerDatabase"&vbCr&_
"Openanotherfile?","ReadudlFile",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Error)

IfnAnswer=DialogResult.YesThen
ReadFromFile()
SplitConnectionString()
ExitSub
Else
DimstrDescriptionAsString="ConnectionFailed."&vbCr&"ClosingApplication"
Err.Raise(vbObjectError+999,,strDescription)
EndIf
本文地址:
http://www.45fan.com/a/question/69733.html