Connecting to SQL Server using windows authentication

C#asp.netSqlSql Server

C# Problem Overview


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)

C# Solutions


Solution 1 - C#

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"

Solution 2 - C#

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>

Solution 3 - C#

Check out www.connectionstrings.com for a ton of samples of proper connection strings.

In your case, use this:

Server=localhost;Database=employeedetails;Integrated Security=SSPI

Update: obviously, the service account used to run ASP.NET web apps doesn't have access to SQL Server, and judging from that error message, you're probably using "anonymous authentication" on your web site.

So you either need to add this account IIS APPPOOL\ASP.NET V4.0 as a SQL Server login and give that login access to your database, or you need to switch to using "Windows authentication" on your ASP.NET web site so that the calling Windows account will be passed through to SQL Server and used as a login on SQL Server.

Solution 4 - C#

You have to add a connectionString within your Web.config file as

<connectionStrings>
    <add name="ASPNETConnectionString" connectionString="Data Source=SONU\SA;Initial Catalog=ASPNET;Integrated Security=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>

Then Write your SQL connection string as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class WebPages_database : System.Web.UI.Page
{
	SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ASPNETConnectionString"].ToString());
	SqlDataAdapter da;
	DataSet ds;
	
	protected void Page_Load(object sender, EventArgs e)
	{
	}

	protected void btnAdmnNumber_Click(object sender, EventArgs e)
	{
		string qry = "select * from Table";
		da = new SqlDataAdapter(qry, con);
		ds = new DataSet();
		da.Fill(ds);

		GridView1.DataSource = ds;
		GridView1.DataBind();
	}
}

For more Information please follow this link How To:Connect to SQl with windows Authentication

SQL Server with windows authentication

Solution 5 - C#

I was facing the same issue and the reason was single backslah. I used double backslash in my "Data source" and it worked

connetionString = "Data Source=localhost\\SQLEXPRESS;Database=databasename;Integrated Security=SSPI";

Solution 6 - C#

This worked for me:

in web.config file;

<add name="connectionstring name " connectionstring="server=SQLserver name; database= databasename; integrated security = true"/>

Solution 7 - C#

Just replace the first line with the below;

SqlConnection con = new SqlConnection("Server=localhost;Database=employeedetails;Trusted_Connection=True");

Regards.

Solution 8 - C#

Use this code:

		SqlConnection conn = new SqlConnection();
		conn.ConnectionString = @"Data Source=HOSTNAME\SQLEXPRESS; Initial Catalog=DataBase; Integrated Security=True";
		conn.Open();
		MessageBox.Show("Connection Open  !");
		conn.Close();

Solution 9 - C#

use this code

Data Source=.;Initial Catalog=master;Integrated Security=True

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHARI KRISHNAView Question on Stackoverflow
Solution 1 - C#ps2goatView Answer on Stackoverflow
Solution 2 - C#AjayView Answer on Stackoverflow
Solution 3 - C#marc_sView Answer on Stackoverflow
Solution 4 - C#Ravibhushan KumarView Answer on Stackoverflow
Solution 5 - C#ÇAğrı KeskinView Answer on Stackoverflow
Solution 6 - C#santhoshView Answer on Stackoverflow
Solution 7 - C#Khaled KabbachView Answer on Stackoverflow
Solution 8 - C#LuispaView Answer on Stackoverflow
Solution 9 - C#sourav adhyaView Answer on Stackoverflow