ChatGPT解决这个技术问题 Extra ChatGPT

Connecting to SQL Server using windows authentication

When I was trying to connect to SQL Server using the following code:

SqlConnection con = new SqlConnection("Server=localhost,Authentication=Windows Authentication, Database=employeedetails");
con.Open();
SqlCommand cmd;
string s = "delete employee where empid=103";

I get the following error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)

SQL isn't configured to allow remote connections.
it's on the localhost, not much remote about that.
Good point. Connection string is wrong.
try this connecionstring ("server=servername/Instancename; database=employeedetails;integrated security=true") write your server name and instance name of your server in servername and instance name

p
ps2goat

A connection string for SQL Server should look more like: "Server= localhost; Database= employeedetails; Integrated Security=True;"

If you have a named instance of SQL Server, you'll need to add that as well, e.g., "Server=localhost\sqlexpress"


Login failed for user 'IIS APPPOOL\ASP.NET V4.0'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'IIS APPPOOL\ASP.NET V4.0'. Source Error: Line 18: { Line 19: SqlConnection con = new SqlConnection("Server= localhost; Database= employeedetails; Integrated Security=True"); Line 20: con.Open();
your user name doesn't look correct. your computer name is 'IIS APPPOOL'? and your user name is 'ASP.NET V4.0'? Please post your connection string with the actual username and password replaced with 'xxxx' to mask the value. THen we can help you better.
The application pool of your web site must working from some account wich have been added as login on the SQL Server and has some permissions on your database. Application pool in Advanced settings has the option as "Identity" - here you should specify your account. After that your connection with "Integrated Security=True" must working fine.
A
Ajay

Your connection string is wrong

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

m
marc_s

Check out www.connectionstrings.com for a ton of samples of proper connection strings.

In your case, use this:

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

Update: obviously, the service account used to run ASP.NET web apps doesn't have access to SQL Server, and judging from that error message, you're probably using "anonymous authentication" on your web site.

So you either need to add this account IIS APPPOOL\ASP.NET V4.0 as a SQL Server login and give that login access to your database, or you need to switch to using "Windows authentication" on your ASP.NET web site so that the calling Windows account will be passed through to SQL Server and used as a login on SQL Server.


Login failed for user 'IIS APPPOOL\ASP.NET V4.0'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'IIS APPPOOL\ASP.NET V4.0'. Source Error: Line 18: { Line 19: SqlConnection con = new SqlConnection("Server= localhost; Database= employeedetails; Integrated Security=True"); Line 20: con.Open()
If a site is using Windows Authentication and Integrated Security=SSPI is in the connectionString, how precisely would you go about making it pass the Windows account through to the SQL server?
m
martijnn2008

You have to add a connectionString within your Web.config file as

<connectionStrings>
    <add name="ASPNETConnectionString" connectionString="Data Source=SONU\SA;Initial Catalog=ASPNET;Integrated Security=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>

Then Write your SQL connection string as below:

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

For more Information please follow this link How To:Connect to SQl with windows Authentication

SQL Server with windows authentication


Ç
ÇAğrı Keskin

I was facing the same issue and the reason was single backslah. I used double backslash in my "Data source" and it worked

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

J
JeremyW

This worked for me:

in web.config file;

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

K
Khaled Kabbach

Just replace the first line with the below;

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

Regards.


L
Luispa

Use this code:

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

F
Fluffeh

use this code

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