简体   繁体   中英

How to connect to SQL Server with a different windows account

I have a C# windows application from which I need to connect to a SQL Server database and access the tables, with a different windows account.(Other than the logged in account)

What is the best approach for this.

Most commonly for SQL Server, I would use integrated security, so a connection string like this:

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

If you need to connect as a different user, which you're not logged in as, then you need to specify the username / password combination in the connection string, like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM