Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to SQL Server using windows authentication

When I was trying to connect to SQL Server using the following code:

SqlConnection con = new SqlConnection("Server=localhost,Authentication=Windows Authentication, Database=employeedetails"); con.Open(); SqlCommand cmd; string s = "delete employee where empid=103"; 

I get the following error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)

like image 517
HARI KRISHNA Avatar asked Sep 04 '13 04:09

HARI KRISHNA


People also ask

Can a SQL Server login use Windows Authentication?

A connection made using Windows Authentication is sometimes called a trusted connection, because SQL Server trusts the credentials provided by Windows. By using Windows Authentication, Windows groups can be created at the domain level, and a login can be created on SQL Server for the entire group.

How does Windows Authentication work in SQL Server?

Windows authentication means the account resides in Active Directory for the Domain. SQL Server knows to check AD to see if the account is active, password works, and then checks what level of permissions are granted to the single SQL server instance when using this account.

How do I connect to SQL Server with SQL authentication?

Right-click the server you wish to modify and then click Properties. Select the Security Page. Under the Server authentication heading choose either the desired authentication: Windows Authentication or SQL Server and Windows Authentication mode. Click OK.

Could not connect to SQL Server using Windows Authentication?

Resolution to SQL error code 18456Go to the Properties of the selected server (from the right-click menu). Now go to the Security page and under Server authentication choose the option SQL Server and Window Authentication mode. Now expand Security > Logins and right-click the server name, and select Properties.


2 Answers

A connection string for SQL Server should look more like: "Server= localhost; Database= employeedetails; Integrated Security=True;"

If you have a named instance of SQL Server, you'll need to add that as well, e.g., "Server=localhost\sqlexpress"

like image 190
ps2goat Avatar answered Oct 05 '22 08:10

ps2goat


Your connection string is wrong

<connectionStrings>    <add name="ConnStringDb1" connectionString="Data Source=localhost\SQLSERVER;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" /> </connectionStrings> 
like image 34
Ajay Avatar answered Oct 05 '22 06:10

Ajay