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

时间:2013-09-04 04:42:36

标签: 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是否配置为允许远程连接。 (提供者:SQL网络接口,错误:25 - 连接字符串无效)

9 个答案:

答案 0 :(得分:53)

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

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

答案 1 :(得分:18)

您的连接字符串错误

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

答案 2 :(得分:9)

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

在您的情况下,请使用:

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

更新很明显,用于运行ASP.NET网络应用的服务帐户无法访问SQL Server,并且从该错误消息判断,您可能正在使用“匿名身份验证”在您的网站上。

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

答案 3 :(得分:2)

您必须在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

答案 4 :(得分:1)

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

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

答案 5 :(得分:0)

这对我有用:

在web.config文件中;

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

答案 6 :(得分: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();

答案 7 :(得分:0)

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

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

致谢。

答案 8 :(得分:-4)

使用此代码

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