C# 使用 Windows 身份验证连接到 SQL Server
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18605533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting to SQL Server using windows authentication
提问by HARI KRISHNA
When I was trying to connect to SQL Server using the following code:
当我尝试使用以下代码连接到 SQL Server 时:
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 Server 建立连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供程序:SQL 网络接口,错误:25 - 连接字符串无效)
回答by ps2goat
A connection string for SQL Server should look more like: "Server= localhost; Database= employeedetails; Integrated Security=True;"
SQL Server 的连接字符串应该更像: "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"
如果您有 SQL Server 的命名实例,则还需要添加它,例如, "Server=localhost\sqlexpress"
回答by marc_s
Check out www.connectionstrings.comfor a tonof samples of proper connection strings.
查看www.connectionstrings.com以获取大量正确连接字符串的示例。
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.
更新:显然,用于运行 ASP.NET Web 应用程序的服务帐户无权访问 SQL Server,从该错误消息判断,您可能在您的网站上使用了“匿名身份验证”。
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.
因此,您需要将此帐户添加IIS APPPOOL\ASP.NET V4.0
为 SQL Server 登录名并授予该登录名访问您的数据库,或者您需要在 ASP.NET 网站上切换到使用“Windows 身份验证”,以便调用 Windows 帐户通过到 SQL Server 并用作 SQL Server 上的登录名。
回答by 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>
回答by sourav adhya
use this code
使用此代码
Data Source=.;Initial Catalog=master;Integrated Security=True
回答by Ravibhushan Kumar
You have to add a connectionString
within your Web.config file as
你必须connectionString
在你的 Web.config 文件中添加一个
<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:
然后编写您的 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();
}
}
For more Information please follow this link How To:Connect to SQl with windows Authentication
有关更多信息,请点击此链接 How To:Connect to SQl with windows Authentication
回答by santhosh
This worked for me:
这对我有用:
in web.config file;
在 web.config 文件中;
<add name="connectionstring name " connectionstring="server=SQLserver name; database= databasename; integrated security = true"/>