调用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
PublicClassConnData
Privatem_strFileNameAsString
Privatem_strConnectionTextAsString
Privatem_strPesistSecurityAsString
Privatem_strIntegradedSecurityAsString
Privatem_strReconstructedAsString
Privatem_strDefaultFilenameAsString
Privatem_strDatasourceAsString
Privatem_strPasswordAsString
Privatem_strUserIDAsString
Privatem_strCatalogAsString'Database
PublicEventCloseApplication()
PublicSubNew()
m_strDefaultFilename="DataLink.udl"
ReadFromFile()
SplitConnectionString()
EndSub
PublicFunctionGetSQLConnectionString()AsString
ReconstructSQLConnectionString()
Returnm_strReconstructed
EndFunction
PrivateSubReconstructSQLConnectionString()
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文件中,读取的字符串
ImportsSystem.IO
PublicClassConnData
Privatem_strFileNameAsString
Privatem_strConnectionTextAsString
Privatem_strPesistSecurityAsString
Privatem_strIntegradedSecurityAsString
Privatem_strReconstructedAsString
Privatem_strDefaultFilenameAsString
Privatem_strDatasourceAsString
Privatem_strPasswordAsString
Privatem_strUserIDAsString
Privatem_strCatalogAsString'Database
PublicEventCloseApplication()
PublicSubNew()
m_strDefaultFilename="DataLink.udl"
ReadFromFile()
SplitConnectionString()
EndSub
PublicFunctionGetSQLConnectionString()AsString
ReconstructSQLConnectionString()
Returnm_strReconstructed
EndFunction
PrivateSubReconstructSQLConnectionString()
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文件中,读取的字符串
本文地址:http://www.45fan.com/a/question/69733.html