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)
Solution 1:
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:
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:
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:
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:
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";