Question:
How to use C# to connect to a db server with Windows Auth, but different credentials?
?
2011-12-08 15:42:19 UTC
Hey all,

I'm writing a program in C# to connect to a database on a remote server. The server uses Windows Authentication.

At the moment to connect to the server in Sql Management Studios I use 'runas.exe'.

Something to the effect of:

runas /netonly /user:DIFFERENT_DOMAIN\SAME_USERNAME "C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Ssms.exe"

My question is, in the program I'm making in C#. How can I get my program to connect to the same database without using runas.exe + arguments?
Four answers:
?
2011-12-09 00:42:51 UTC
That's true.. you would get a Login Failed error since the Connection String has command to use default Windows Authentication ( ;Integrated Security=true ).



To tell you, this Connection String can be manipulated to accept UserName & Password of your desire, but that translates into SQL Authentication when App connects to Database.



So, you have 2 options -



1. Enable SQL Authentication on the Server. Create a SQL Login and Modify this Connection String (Replace ;Integrated Security=true with UserName='';Password='')



2. Use RunAs to open the Visual Studio ( or any other App you are using to develop ) and first complete developing it. Later, after you generate a .Exe ( in case of a Windows App) or any other... you can either make it execute using Windows Task Scheduler ( that accepts the Login & password ) or Use a SQL Server Job ( that also accepts the Login & password ) to execute.





-- In 'thoughts'...

Lonely Rogue.
?
2016-08-27 23:28:15 UTC
2
mirafuentes
2016-10-24 06:23:02 UTC
definite that's attainable. verify to verify that a million. the DB service is working on the 2003 server, 2. you're making use of the marvelous credentials (eg. username & password) 3. that your packet site visitors isn't being blocked to the port assigned to the listening DB service. If all else fails attempt guy mysl. solid luck
Gauri
2011-12-08 15:47:43 UTC
You must use class SqlConnection from System.Data.SqlClient namespace.



Sqlconnection sc = new Sqlconnection();

sc.ConnectionString = "Data Source= ;Initial Catalog= ;Integrated Security=true";

sc.Open();



Give the name of your sql server in data source and database you want to connect to in initial catalog.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...