programing

윈도우즈 인증을 사용하여 SQL Server에 연결

lovejava 2023. 6. 8. 19:07

윈도우즈 인증을 사용하여 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 - 연결 문자열이 잘못되었습니다)

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 웹 앱을 실행하는 데 사용된 서비스 계정은 SQL Server에 액세스할 수 없으며, 해당 오류 메시지로 미루어 볼 때 웹 사이트에서 "익명 인증"을 사용하고 있는 것 같습니다.

그래서 당신은 이 계정을 추가해야 합니다.IIS APPPOOL\ASP.NET V4.0SQL Server 로그인으로 데이터베이스에 대한 액세스 권한을 부여하거나 ASP.NET 웹 사이트에서 "Windows 인증"으로 전환하여 호출 중인 Windows 계정이 SQL Server로 전달되고 SQL Server에서 로그인으로 사용되도록 해야 합니다.

다음을 추가해야 합니다.connectionString웹.config 파일 내에서 다음과 같이 사용할 수 있습니다.

<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();
    }
}

자세한 내용은 다음 링크를 참조하십시오. 방법:윈도우즈 인증을 사용하여 SQL에 연결

윈도우즈 인증을 사용하는 SQL Server

저는 같은 문제에 직면하고 있었고 그 이유는 싱글 백슬래시였습니다."데이터 소스"에서 이중 백슬래시를 사용했는데 작동했습니다.

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

이것은 저에게 효과가 있었습니다.

web.config 파일에 있습니다.

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

첫 번째 줄을 아래로 바꾸기만 하면 됩니다.

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

안부 전해요.

다음 코드 사용:

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

이 코드를 사용합니다.

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

언급URL : https://stackoverflow.com/questions/18605533/connecting-to-sql-server-using-windows-authentication