簡體   English   中英

使用 Windows 身份驗證連接到 SQL Server

[英]Connecting to SQL Server using windows authentication

當我嘗試使用以下代碼連接到 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 的連接字符串應該更像: "Server= localhost; Database= employeedetails; Integrated Security=True;"

如果您有 SQL Server 的命名實例,則還需要添加它,例如, "Server=localhost\\sqlexpress"

您的連接字符串錯誤

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

查看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 上的登錄名。

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

帶有 Windows 身份驗證的 SQL Server

這對我有用:

在 web.config 文件中;

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

我面臨同樣的問題,原因是單反斜杠。 我在“數據源”中使用了雙反斜杠並且它有效

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

使用此代碼:

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

只需用下面的替換第一行;

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

問候。

使用此代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM