我正在尝试连接到非本地的SQL Server数据库。我有数据源和初始目录 - 没有问题。但需要将 Integrated Security 更改为False并插入SQL Server凭据。
有没有人知道如何将它放在连接字符串中?
此外,是否有人知道如何处理 SecureStrings ?
到目前为止,这是我的代码:
Dim pwd As New SecureString("Password")
Dim cred As New SqlCredential("Username", pwd)
Dim sql As New SqlConnection("Data Source=OnlineServer;Initial Catalog=DatabaseName;Integrated Security=False")
答案 0 :(得分:3)
请看这里:SQL Connection Strings希望找到你需要的那个。这将为您提供基础知识。
要使SQL帐户凭据保密,您应该加密web.config中的<connection strings>
部分。这样做:
aspnet_regiis -pe "connectionStrings" -app "OnlineServer" -prov "DataProtectionConfigurationProvider"
使用ConfigurationManager检索连接字符串将自动解密字符串
Dim connectionString = ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString
这是Microsoft Link,可以进一步解释。
答案 1 :(得分:0)
我找到了我需要做的以及如何处理安全字符串。
以下是为将来挣扎的所有人提供的代码段:
Imports System.Data.SqlClient
Imports System.Net.Mail
Imports System.Security
Public Module secure
Public Function sql()
Dim pass As String = "Password"
Dim pwd As SecureString = New SecureString()
For Each ch As Char In pass
pwd.AppendChar(ch)
Next
pwd.MakeReadOnly()
Dim cred As New SqlCredential("SQL_Login", pwd)
Dim conn As New SqlConnection("Server=Database_Name;Initial Catalog=Database_Address;Integrated Security=False", cred)
Return conn
End Function
End Module
Public Class sqlCommunications
Dim sql As New SqlConnection
Dim sqlcom As New SqlCommand
Public Sub start()
sql = secure.sql
sqlcom.Connection = sql
sql.Open()
sql.Close()
End Sub
End Class