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

81

当我试图使用以下代码连接到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 Network Interfaces,错误: 25 - 连接字符串无效)


SQL没有配置允许远程连接。 - Greg
它在本地主机上,没有太多远程的内容。 - ps2goat
好的观点。连接字符串是错误的。 - Greg
2
尝试使用以下连接字符串:("server=servername/Instancename; database=employeedetails;integrated security=true") 在servername和instancename中写入您的服务器名称和实例名称。 - Vikas Rana
9个回答

93

SQL Server的连接字符串应该长这样:"Server= localhost; Database= employeedetails; Integrated Security=True;"

如果你有一个命名的SQL Server实例,你需要把它添加进去,例如:"Server=localhost\sqlexpress"


1
用户“IIS APPPOOL\ASP.NET V4.0”登录失败。描述:当前Web请求执行期间发生未处理的异常。请查看堆栈跟踪以获取有关错误的更多信息以及其在代码中的起源。异常详细信息:System.Data.SqlClient.SqlException:用户“IIS APPPOOL\ASP.NET V4.0”登录失败。源错误:第18行: { 第19行:SqlConnection con = new SqlConnection(“Server = localhost; Database = employeedetails; Integrated Security = True”); 第20行:con.Open(); - HARI KRISHNA
2
你的用户名看起来不正确。你的计算机名是'IIS APPPOOL'?而你的用户名是'ASP.NET V4.0'?请发布你的连接字符串,并将实际的用户名和密码替换为“xxxx”以掩盖其值。然后我们可以更好地帮助你。 - ps2goat
2
您的网站应用程序池必须以已添加为 SQL Server 登录的某个帐户身份运行,并具有对数据库的一些权限。 高级设置中的应用程序池选项为“标识” - 在此处您应该指定您的帐户。 之后,使用“Integrated Security=True”建立的连接必须正常工作。 - Alexander Shapkin

23

你的连接字符串有误

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

18

如果你需要合适的连接字符串样例,可以查看www.connectionstrings.com

对于你的情况,请使用以下内容:

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

更新:很明显,用于运行ASP.NET Web应用程序的服务帐户没有访问SQL Server的权限,并且根据该错误消息判断,您可能正在Web站点上使用“匿名身份验证”。

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


登录失败,用户为 'IIS APPPOOL\ASP.NET V4.0'。描述:当前 Web 请求执行期间发生未处理的异常。请查看堆栈跟踪以获取有关错误的更多信息以及其在代码中的来源。异常详细信息:System.Data.SqlClient.SqlException: 登录失败,用户为 'IIS APPPOOL\ASP.NET V4.0'。源错误:第 18 行:{ 第 19 行:SqlConnection con = new SqlConnection("Server= localhost; Database= employeedetails; Integrated Security=True"); 第 20 行:con.Open() - HARI KRISHNA
1
如果一个网站正在使用Windows身份验证,并且connectionString中包含“Integrated Security=SSPI”,您将如何精确地使其通过Windows帐户传递到SQL服务器? - 15ee8f99-57ff-4f92-890c-b56153

4

您需要在Web.config文件中添加一个connectionString,例如:

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

接下来将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();
    }
}

更多信息请点击以下链接 如何使用 Windows 身份验证连接到 SQl

Windows 身份验证访问 SQL Server


4

我曾面临相同的问题,原因是单个反斜杠。我在“数据源”中使用了双反斜杠,问题得到解决。

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

1
只需将第一行替换为以下内容;
SqlConnection con = new SqlConnection("Server=localhost;Database=employeedetails;Trusted_Connection=True");

问候。


1
这对我有用:
在web.config文件中;
<add name="connectionstring name " connectionstring="server=SQLserver name; database= databasename; integrated security = true"/>

0
使用这个代码:
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = @"Data Source=HOSTNAME\SQLEXPRESS; Initial Catalog=DataBase; Integrated Security=True";
        conn.Open();
        MessageBox.Show("Connection Open  !");
        conn.Close();

-3
请使用这段代码。
Data Source=.;Initial Catalog=master;Integrated Security=True

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接