Shima.Y
Shima.Y

Reputation: 383

How to change user of windows authentication connection string in C#

When I want to connect to SQL Server 2008 via SSMS in windows authentication mode I get the following error:

login failed for user UserDomainName\UserName

I installed my SQL Server in mix mode and set user to NT Authority\system , how can I create a connection string with "UserDomainName\UserName" user not with "NT Authority\system" user in C#?

Upvotes: 4

Views: 12288

Answers (2)

Salah Akbari
Salah Akbari

Reputation: 39966

It should something like this:

string address = "data source=(local);initial catalog=yourDB;user id=sa;password=yourPass";

Or:

string address = "Data Source=(local);Initial Catalog=yourDB;Integrated Security=True";

You can check this for more: SQL Server 2008 connection strings

Edit: Integrated Security will always use the current identity. So you won't be able to establish a connection with Windows Authentication with credentials from another domain. You need to create an SQL Server login for UserDomainName\UserName like this:

Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI; User ID=UserDomainName\UserName;Password=myPassword; 

Upvotes: 0

Rahul
Rahul

Reputation: 77906

Store the connection string in Web.Config or App.Config file

<connectionStrings>
<Add name="CS" ConnectionString="DataSource=.;Database=Sample;Integrated Security=SSPI" ProviderName="System.Data.SqlClient"/>
</connectionStrings>

then you can read the connection string property using System.Configuration namespace (dll)

string conStr = ConfigurationManager.ConnectionStrings["CS"].ConnectionString;

how can I create a connection string with "UserDomainName\UserName" user not with "NT Authority\system" user in C#?

Then use SQL Authentication mode and not windows authentication

ConnectionString="DataSource=.;Database=Sample;user id = test; password=test"

Upvotes: 1

Related Questions