在示例中,我有以下字符串:
[OLEDB]
;此行之后的所有内容都是OLE DB initstring
Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;User ID=sa;Initial Catalog=BMC;Data Source=
。
我想在vb 2005中只使用子字符串密码和用户ID。
怎么做?
我需要的结果:Password=123456 and User ID=sa
。
答案 0 :(得分:0)
由于这是一个连接字符串,您应该可以使用OleDbConnectionStringBuilder
来解析它。
一个例子是:
Dim builder As New OleDbConnectionStringBuilder(connectionString)
' builder("User ID") contains User ID
' builder("Password") contains Password
答案 1 :(得分:0)
如果你想用旧式字符串解析,那么:
Dim data As String = "[oledb] ; Everything after this line is an OLE DB initstring" & vbCrLf _
& "Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;User ID=sa;Initial Catalog=BMC;Data Source="
Dim password As String = ""
Dim userID As String = ""
Dim semiColonIndex As Integer
Dim passIndex As Integer = data.IndexOf("Password=")
If passIndex > 0 Then
semiColonIndex = data.IndexOf(";", passIndex)
If semiColonIndex > 0 Then
password = data.Substring(passIndex + "Password=".Length, semiColonIndex - passIndex - "Password=".Length)
Debug.Print(password)
End If
End If
Dim userIndex As Integer = data.IndexOf("User ID=")
If userIndex > 0 Then
semiColonIndex = data.IndexOf(";", userIndex)
If semiColonIndex > 0 Then
userID = data.Substring(userIndex + "User ID=".Length, semiColonIndex - userIndex - "User ID=".Length)
Debug.Print(userID)
End If
End If
答案 2 :(得分:0)
尝试如下,它会帮助你...
对于使用SQLClient
的SQL连接,然后尝试如下...
Imports System.Data.SqlClient
Dim constring As String
constring = "Password=123456;Persist Security Info=True;User ID=sa;Initial Catalog=BMC;Data Source="
Dim builder As New SqlConnectionStringBuilder(constring)
Console.WriteLine("Username : " + builder.UserID)
Console.WriteLine("Password : " + builder.Password)
使用OLEDB Provider
然后尝试如下...
Imports System.Data.OleDb
Dim oledbbuilder As New OleDbConnectionStringBuilder("Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;User ID=sa;Initial Catalog=BMC;Data Source=")
Console.WriteLine("Username : " + oledbbuilder("User ID").ToString)
Console.WriteLine("Password : " + oledbbuilder("Password").ToString)