使用windows身份验证连接到SQL Server

时间:2021-11-26 01:36:48

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

当我试图使用以下代码连接到SQL Server时

     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)

在建立到SQL Server的连接时发生与网络相关或特定于实例的错误。没有找到或无法访问服务器。验证实例名是否正确,并将SQL Server配置为允许远程连接。(提供程序:SQL网络接口,错误:25 -连接字符串无效)

5 个解决方案

#1


34  

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

SQL Server的连接字符串应该更像:“Server= localhost;数据库= employeedetails;综合安全= True;”

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

如果您有一个命名的SQL Server实例,您还需要添加它,例如,“Server=localhost\sqlexpress”

#2


12  

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>

#3


7  

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

查看www.connectionstrings.com来获得大量正确连接字符串的示例。

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.

更新:显然,用于运行ASP的服务帐户。NET web应用程序不能访问SQL Server,从错误消息判断,您可能正在web站点上使用“匿名身份验证”。

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.

所以你需要添加这个帐户IIS APPPOOL\ASP。NET V4.0作为SQL服务器登录,并提供对数据库的登录访问,或者您需要切换到使用ASP上的“Windows身份验证”。NET web站点,以便调用Windows帐户将被传递到SQL Server并在SQL Server上作为登录。

#4


0  

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

您必须在Web中添加connectionString。配置文件

<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:

然后编写SQL连接字符串如下:

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

有关更多信息,请按照以下链接:如何使用windows身份验证连接到SQl

SQL Server with windows authentication

具有windows身份验证的SQL Server

#5


-3  

use this code

使用这个代码

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

#1


34  

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

SQL Server的连接字符串应该更像:“Server= localhost;数据库= employeedetails;综合安全= True;”

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

如果您有一个命名的SQL Server实例,您还需要添加它,例如,“Server=localhost\sqlexpress”

#2


12  

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>

#3


7  

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

查看www.connectionstrings.com来获得大量正确连接字符串的示例。

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.

更新:显然,用于运行ASP的服务帐户。NET web应用程序不能访问SQL Server,从错误消息判断,您可能正在web站点上使用“匿名身份验证”。

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.

所以你需要添加这个帐户IIS APPPOOL\ASP。NET V4.0作为SQL服务器登录,并提供对数据库的登录访问,或者您需要切换到使用ASP上的“Windows身份验证”。NET web站点,以便调用Windows帐户将被传递到SQL Server并在SQL Server上作为登录。

#4


0  

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

您必须在Web中添加connectionString。配置文件

<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:

然后编写SQL连接字符串如下:

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

有关更多信息,请按照以下链接:如何使用windows身份验证连接到SQl

SQL Server with windows authentication

具有windows身份验证的SQL Server

#5


-3  

use this code

使用这个代码

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