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

C# 使用windows身份验证连接到SQL Server,c#,asp.net,sql,sql-server,C#,Asp.net,Sql,Sql Server,当我尝试使用以下代码连接到SQL Server时: SqlConnection con = new SqlConnection("Server=localhost,Authentication=Windows Authentication, Database=employeedetails"); con.Open(); SqlCommand cmd; string s = "delete employee where empid=103"; 我得到以下错误: 建立与SQL Server的连接时

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

SqlConnection con = new SqlConnection("Server=localhost,Authentication=Windows Authentication, Database=employeedetails");
con.Open();
SqlCommand cmd;
string s = "delete employee where empid=103";
我得到以下错误:

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


SQL Server的连接字符串应该更像:
“Server=localhost;Database=employeedetails;integratedsecurity=True;”

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

请查看大量正确连接字符串的示例

在您的情况下,请使用以下命令:

Server=localhost;Database=employeedetails;Integrated Security=SSPI
更新:显然,用于运行ASP.NET web应用程序的服务帐户没有访问SQL Server的权限,从该错误消息判断,您可能正在网站上使用“匿名身份验证”


因此,您需要将此帐户
IIS APPPOOL\ASP.NET V4.0
添加为SQL Server登录名,并授予该登录名对数据库的访问权限,或者需要切换到使用“Windows身份验证”在ASP.NET网站上,以便调用的Windows帐户将传递到SQL Server并用作SQL Server上的登录名。

您的连接字符串错误

<connectionStrings>
   <add name="ConnStringDb1" connectionString="Data Source=localhost\SQLSERVER;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

使用此代码

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

您必须在Web.config文件中添加一个
connectionString
,如下所示:

<connectionStrings>
    <add name="ASPNETConnectionString" connectionString="Data Source=SONU\SA;Initial Catalog=ASPNET;Integrated Security=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>
有关更多信息,请点击此链接

这对我很有效:

在web.config文件中

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

我也面临同样的问题,原因是单背拉。 我在“数据源”中使用了双反斜杠,效果很好

connetionString = "Data Source=localhost\\SQLEXPRESS;Database=databasename;Integrated Security=SSPI";
使用此代码:

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

只需将第一行替换为以下内容

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

注意。

SQL没有配置为允许远程连接。它在本地主机上,不太远程。这一点很好。连接字符串错误。请尝试此连接字符串(“server=servername/Instancename;database=employeedetails;integratedsecurity=true”)在servername和instance name中写入服务器的服务器名称和实例名称登录用户“IIS APPPOOL\ASP.NET V4.0”失败。描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。异常详细信息:System.Data.SqlClient.SqlException:用户“IIS APPPOOL\ASP.NET V4.0”登录失败。源错误:第18行:{19行:SqlConnection con=newsqlconnection(“Server=localhost;Database=employeedetails;integratedsecurity=True”);第20行:con.Open();您的用户名看起来不正确。您的计算机名为“IIS APPPOOL”?而您的用户名为“ASP.NET V4.0”?请发布连接字符串,用实际用户名和密码替换为“xxxx”以屏蔽该值。这样我们可以更好地帮助您。您网站的应用程序池必须使用已添加为登录SQL Server并对您的数据库具有某些权限。高级设置中的应用程序池具有“标识”选项-在此您应该指定您的帐户。然后,您可以使用“Integrated Security=True”连接必须正常工作。用户“IIS APPPOOL\ASP.NET V4.0”的登录失败。说明:在执行当前web请求期间发生未经处理的异常。有关错误及其源代码的详细信息,请查看堆栈跟踪。异常详细信息:System.Data.SqlClient.SqlException:用户“IIS”的登录失败APPPOOL\ASP.NET V4.0'。源错误:第18行:{19行:SqlConnection con=new SqlConnection(“服务器=localhost;数据库=employeedetails;集成安全=True”);第20行:con.Open()如果站点正在使用Windows身份验证,并且连接字符串中有
Integrated Security=SSPI
,您将如何精确地让它将Windows帐户传递到SQL server?
SqlConnection con = new SqlConnection("Server=localhost;Database=employeedetails;Trusted_Connection=True");