SqlConnectionStringBuilder.IntegratedSecurity 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定布林值,指出是否在連接中指定使用者 ID 和密碼 (false
時) 或目前 Windows 帳戶認證是否用於驗證 (true
時)。
public:
property bool IntegratedSecurity { bool get(); void set(bool value); };
public bool IntegratedSecurity { get; set; }
member this.IntegratedSecurity : bool with get, set
Public Property IntegratedSecurity As Boolean
屬性值
IntegratedSecurity 屬性的值,如未提供任何值,則為 false
。
範例
下列範例會轉換現有的連接字串,將其從使用 SQL Server 驗證轉換成使用整合式安全性。 此範例執行工作的方式是從連接字串中移除使用者名稱和密碼,然後設定 IntegratedSecurity 物件的 SqlConnectionStringBuilder 屬性。
注意
這個範例包含了密碼,可示範 SqlConnectionStringBuilder 如何搭配連接字串使用。 我們建議在您的應用程式中使用 Windows 驗證。 如果您必須使用密碼,請勿在您的應用程式中包含硬式編碼的密碼。
using System.Data.SqlClient;
class Program
{
static void Main()
{
try
{
string connectString =
"Data Source=(local);User ID=ab;Password=MyPassword;" +
"Initial Catalog=AdventureWorks";
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder(connectString);
Console.WriteLine("Original: " + builder.ConnectionString);
// Use the Remove method
// in order to reset the user ID and password back to their
// default (empty string) values. Simply setting the
// associated property values to an empty string won't
// remove them from the connection string; you must
// call the Remove method.
builder.Remove("User ID");
builder.Remove("Password");
// Turn on integrated security:
builder.IntegratedSecurity = true;
Console.WriteLine("Modified: " + builder.ConnectionString);
using (SqlConnection connection =
new SqlConnection(builder.ConnectionString))
{
connection.Open();
// Now use the open connection.
Console.WriteLine("Database = " + connection.Database);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Press any key to finish.");
Console.ReadLine();
}
}
Imports System.Data.SqlClient
Module Module1
Sub Main()
Try
Dim connectString As String = _
"Data Source=(local);User ID=ab;Password=MyPassword;" & _
"Initial Catalog=AdventureWorks"
Dim builder As New SqlConnectionStringBuilder(connectString)
Console.WriteLine("Original: " & builder.ConnectionString)
' Use the Remove method
' in order to reset the user ID and password back to their
' default (empty string) values. Simply setting the
' associated property values to an empty string won't
' remove them from the connection string; you must
' call the Remove method.
builder.Remove("User ID")
builder.Remove("Password")
' Turn on integrated security.
builder.IntegratedSecurity = True
Console.WriteLine("Modified: " & builder.ConnectionString)
Using connection As New SqlConnection(builder.ConnectionString)
connection.Open()
' Now use the open connection.
Console.WriteLine("Database = " & connection.Database)
End Using
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.WriteLine("Press any key to finish.")
Console.ReadLine()
End Sub
End Module
備註
此屬性會對應至連接字串內的 "Integrated Security" 和 "trusted_connection" 索引鍵。
適用於
另請參閱
- ADO.NET 中的連接字串
- ADO.NET 概觀 \(部分機器翻譯\)