web-dev-qa-db-ja.com

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-接続文字列が無効です)

49
HARI KRISHNA

SQL Serverの接続文字列は、次のようになります。"Server= localhost; Database= employeedetails; Integrated Security=True;"

SQL Serverの名前付きインスタンスがある場合は、"Server=localhost\sqlexpress"など、それも追加する必要があります

54
ps2goat

接続文字列が間違っています

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

適切な接続文字列のサンプルのtonについて 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 Webサイトで「Windows認証」を使用して、呼び出し元のWindowsアカウントはSQL Serverにパススルーされ、SQL Serverのログインとして使用されます。

9
marc_s

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 with SQl with windows Authentication

Windows認証を使用するSQL Server

2

これは私のために働いた:

web.configファイル内。

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