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

问题描述 投票:0回答:10

当我尝试使用以下代码连接到 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 - 连接字符串无效)

c# asp.net sql sql-server
10个回答
93
投票

SQL Server 的连接字符串应该类似于:

"Server= localhost; Database= employeedetails; Integrated Security=True;"

如果您有 SQL Server 的命名实例,则还需要添加该实例,例如

"Server=localhost\sqlexpress"


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,从该错误消息来看,您可能在网站上使用“匿名身份验证”。

因此,您需要将此帐户

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


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
投票

这对我有用:

在 web.config 文件中;

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

1
投票

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

SqlConnection con = new SqlConnection("Server=localhost;Database=employeedetails;Trusted_Connection=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();

0
投票

这对我有用

“连接字符串”:{ “myconn”:“服务器= DESKTOP-O9L9733;数据库= BlazorCRUD;Trusted_Connection = True;集成安全性= True;加密= False;信任服务器证书= True” }


-3
投票

使用此代码

Data Source=.;Initial Catalog=master;Integrated Security=True
© www.soinside.com 2019 - 2024. All rights reserved.