ChatGPT解决这个技术问题 Extra ChatGPT

使用 Windows 身份验证连接到 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=servername/Instancename; database=employeedetails;integrated security=true") 在 servername 和 instance name 中写入您的服务器名称和服务器实例名称

p
ps2goat

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

如果您有 SQL Server 的命名实例,则还需要添加它,例如 "Server=localhost\sqlexpress"


用户 '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();
您的用户名看起来不正确。您的计算机名称是“IIS APPPOOL”?并且您的用户名是“ASP.NET V4.0”?请张贴您的连接字符串,并将实际用户名和密码替换为“xxxx”以掩盖该值。那么我们可以为您提供更好的帮助。
您网站的应用程序池必须使用某个帐户工作,该帐户已添加为 SQL Server 上的登录名,并且对您的数据库具有某些权限。高级设置中的应用程序池具有“身份”选项 - 您应该在此处指定您的帐户。之后,您与“Integrated Security=True”的连接必须正常工作。
A
Ajay

您的连接字符串错误

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

m
marc_s

查看 www.connectionstrings.com 以获取 正确连接字符串的示例。

在你的情况下,使用这个:

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

更新:显然,用于运行 ASP.NET Web 应用程序的服务帐户无权访问 SQL Server,从该错误消息来看,您可能在您的网站上使用“匿名身份验证”。

因此,您需要将此帐户 IIS APPPOOL\ASP.NET V4.0 添加为 SQL Server 登录名并授予该登录名访问您的数据库的权限,或者您需要切换到在 ASP.NET 网站上使用“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()
如果一个站点正在使用 Windows 身份验证并且 Integrated Security=SSPI 在 connectionString 中,那么您将如何精确地将 Windows 帐户传递到 SQL 服务器?
m
martijnn2008

您必须在您的 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();
    }
}

如需更多信息,请点击此链接 How To:Connect to SQl with windows Authentication

SQL Server with windows authentication


Ç
ÇAğrı Keskin

我面临同样的问题,原因是单反斜杠。我在“数据源”中使用了双反斜杠并且它有效

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

J
JeremyW

这对我有用:

在 web.config 文件中;

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

K
Khaled Kabbach

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

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

问候。


L
Luispa

使用此代码:

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

F
Fluffeh

使用此代码

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