【问题标题】:Connecting to SQL Server using windows authentication使用 Windows 身份验证连接到 SQL Server
【发布时间】:2013-09-07 11:17:06
【问题描述】:

当我尝试使用以下代码连接到 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 中写下你的服务器名称和实例名称

标签: c# asp.net sql sql-server


【解决方案1】:

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”的连接必须正常工作。
【解决方案2】:

你的连接字符串错误

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

【讨论】:

    【解决方案3】:

    查看 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 服务器?
    【解决方案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();
        }
    }
    

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

    SQL Server with windows authentication

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题,原因是单反斜杠。 我在“数据源”中使用了双反斜杠,它起作用了

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

      【讨论】:

        【解决方案6】:

        这对我有用:

        在 web.config 文件中;

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

        【讨论】:

          【解决方案7】:

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

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

          问候。

          【讨论】:

            【解决方案8】:

            使用此代码:

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

            【讨论】:

              【解决方案9】:

              使用此代码

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

              【讨论】:

                猜你喜欢
                • 2011-04-10
                • 1970-01-01
                • 1970-01-01
                • 2014-07-15
                • 2015-11-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多