【问题标题】:What is the sql connection string I need to use to access localhost\SQLEXPRESS with Windows Authentication or SQL Authentication?使用 Windows 身份验证或 SQL 身份验证访问 localhost\SQLEXPRESS 需要使用什么 sql 连接字符串?
【发布时间】:2011-03-12 16:22:04
【问题描述】:

已经在我的 PC 上安装了 SQL Express,希望能做一些创建表然后修改它们的练习。我在 Visual Studio 中编写了一个网页,基本上是从 SQLEXPRESS 中的表中选择 *,但我永远无法使连接字符串正常工作。请帮忙

我的连接字符串

"数据 源=localhost\SQLEXPRESS;初始 目录=测试;用户 id=xaa9-PC\xaa9;密码=abcd;"

错误信息:

查询是 select * from tblCustomers 其中 username='johndoe' 错误是 用户“x309-PC\x309”登录失败。

描述:未处理的异常 在执行过程中发生 当前的网络请求。请查看 堆栈跟踪以获取有关的更多信息 错误及其起源 代码。

异常详细信息:System.Exception: 查询是 select * from tblCustomers 其中 username='johndoe' 错误是 用户“x309-PC\x309”登录失败。

【问题讨论】:

  • 查看ConnectionStrings.com - 它显示了人类已知的 SQL Server 连接字符串的所有可能组合和变体...

标签: sql-server


【解决方案1】:

尝试使用 Windows 身份验证:

Data Source=localhost\SQLEXPRESS;Initial Catalog=test;Integrated Security=SSPI;

【讨论】:

    【解决方案2】:

    试试这样:

    string connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=test;User Id=x309;Password=abcd;";
    

    还要确保你有enabled SQL authentication

    【讨论】:

    • 这是一个 Windows DOMAIN\USERNAME,所以 SQL 身份验证不可行,您需要集成身份验证。
    • @Ben,使用 Windows 身份验证,您无需在连接字符串中指定用户名和密码。
    • 连接字符串中不需要转义。我假设该字符串不在 C# 中,因为编译器会给 \S 一个Unrecognized escape sequence 错误
    • @Andomar,我认为连接字符串中的\x有问题,因为它被认为是HEX。
    • @Darin Dimitrov:看起来反斜杠在 SQL Server 登录中不是一个允许的字符。所以他可能是在尝试使用Windows身份验证登录,他的PC被称为xaa9-PC
    【解决方案3】:

    如果您将数据连接字符串放在 web.config 文件中,您可以像下面这样指定您的连接:

    <connectionStrings>
    <add name="NorthwindConnString" 
         connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True" 
         providerName="System.Data.SqlClient"/>
    </connectionStrings>
    

    但如果您在基于 c# 的网站中进行硬编码,则必须转义“\”反斜杠:

    "Data Source=.\\\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
    

    即使是 Scott Hanselman 也会忘记this...

    【讨论】:

      【解决方案4】:
      public partial class _Default : System.Web.UI.Page
      {
          SqlConnection con = new SqlConnection("Data Source=SHANU-PC\SQLEXPRESS;Initial Catalog=Anusha;Integrated Security=True");
          protected void Page_Load(object sender, EventArgs e)
          {
              if(!IsPostBack)
              {
      
              con.Open();
              SqlCommand cmd=new SqlCommand("select * from tbl_state",con);
      
              SqlDataAdapter da=new SqlDataAdapter(cmd);
      
              DataTable dt=new DataTable();
              da.Fill(dt);
                  DropDownList1.DataSource = dt;
                  DropDownList1.DataTextField = "sname";
                  DropDownList1.DataValueField = "sid";
                  DropDownList1.DataBind();
      
              con.Close();
              }
      

      【讨论】:

        猜你喜欢
        • 2010-10-05
        • 2012-08-29
        • 2015-12-07
        • 1970-01-01
        • 2011-04-10
        • 1970-01-01
        • 2014-05-08
        • 2013-09-07
        相关资源
        最近更新 更多