2013-07-15 46 views
3

我有一個C#windows應用程序,我需要使用其他windows帳戶連接到SQL Server數據庫並訪問這些表(除登錄帳戶外)如何使用不同的Windows帳戶連接到SQL Server

這是什麼最好的方法。

+0

您可以通過運行其他帳戶下的應用程序,但如果你想通過代碼來做到這一點,你需要使用這個http://stackoverflow.com/questions/1168571/run-code-as-a-different-user-c – MEYWD

回答

-4

最常見的SQL Server,我將使用集成的安全性,所以連接字符串是這樣的:

var connectionString = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=SSPI;", hostName, databaseName); 

如果您需要連接爲不同的用戶,這你沒有登錄爲,然後您需要在連接字符串中指定的用戶名/密碼組合,像這樣:

var connectionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};", hostName, databaseName, anotherUsername, anotherPassword); 
+3

我想OP想要使用Windows身份驗證,但使用不同的上下文而不是當前記錄的用戶,而不是切換到SQL身份驗證。 – Alejandro

相關問題