4

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.

KhanS
  • 1,167
  • 2
  • 12
  • 27
  • You can by running the application under the other account, but if you want to do it by code, you will need to use this http://stackoverflow.com/questions/1168571/run-code-as-a-different-user-c – Mahmoud Darwish Jul 15 '13 at 09:39

1 Answers1

-4

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);
Jon G
  • 4,083
  • 22
  • 27
  • 4
    I guess the OP wants to use Windows authentication, but using a different context rather than the currently logged user, instead of switching to SQL authentication. – Alejandro Jul 17 '13 at 00:19