【问题标题】:SQL Connection String C# ASP NET [closed]SQL 连接字符串 C# ASP NET [关闭]
【发布时间】:2015-07-06 17:39:15
【问题描述】:

所以我正在制作一个提交内容页面,它将用户输入存储到数据库中,这是我目前拥有的代码:

protected void submitData_Click(object sender, EventArgs e)
    {
        string userId = HttpContext.Current.User.Identity.Name;
        int categoryId = Convert.ToInt32(categories.SelectedValue);
        if (categoryId > 0 && content.Text != "" && description.Text != "")
        {
            using (SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C: \Users\Will\Documents\Visual Studio 2015\WebSites\inshort\App_Data\ASPNETDB.MDF";Integrated Security=True"))
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO aspnet_newscontent (author, username, date, category, content, description) VALUES (@author, @username, @date, @category, @content, @description)");
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Connection = connection;
                cmd.Parameters.AddWithValue("@author", nameInput.Text);
                cmd.Parameters.AddWithValue("@username", userId);
                cmd.Parameters.AddWithValue("@date", DateTime.Today);
                cmd.Parameters.AddWithValue("@category", categoryId);
                cmd.Parameters.AddWithValue("@content", content.Text);
                cmd.Parameters.AddWithValue("@description", description.Text);
                connection.Open();
                cmd.ExecuteNonQuery();
            }
        }
        else
        {
            errorLabel.Text = "Please fill in the required fields.";
        }
    }

但是,我收到一条错误消息,指出连接字符串包含无效字符“\”,这是有道理的,但是每当我转到数据库的属性并查看连接字符串属性时,它就是这么说的。
如果有任何更改,我正在使用 Microsoft Sql Server Express 将数据库托管在本地。有人知道如何格式化这些连接字符串吗?

【问题讨论】:

  • 转义字符串中的双引号,对于逐字字符串,使用双 double 引号,例如 @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""C: \Users\Will\Documents\Visual Studio 2015\WebSites\inshort\App_Data\ASPNETDB.MDF"";Integrated Security=True" -- 投票关闭为错字
  • 这篇文章可能会帮助有类似问题的人学习如何正确使用转义字符。
  • 也有很多关于这个的帖子,这个问题可以被认为是stackoverflow.com/questions/1928909/…的副本

标签: c# sql asp.net sql-server connection-string


【解决方案1】:

查看语法高亮。您试图将未转义的双引号放在一个字符串中,这显然会使解析器感到困惑,因为它认为您的字符串更早终止,并且在它之后充满了语法错误。

要在逐字字符串文字(前面带有@)中转义引号,您需要“双”-双引号。像这样:

@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""C:\Users\Will\Documents\Visual Studio 2015\WebSites\inshort\App_Data\ASPNETDB.MDF"";Integrated Security=True"

【讨论】:

  • 这成功了,我会尽快接受这个答案。
  • 你确实应该使用这种格式:Data Source=DataBaseServerName;Initial Catalog=SQLDatabaseName
  • @Chuck:这完全适用于 LocalDB 吗?仅仅为了让连接字符串看起来更清晰而更改整个 SQL 基础架构似乎不是一种很好的努力。
【解决方案2】:

您必须转义 C# 字符串中的某些字符。如果去掉“literal”字符串字符(@),它会是这样的:

using (SqlConnection connection = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\Will\\Documents\\Visual Studio 2015\\WebSites\\inshort\\App_Data\\ASPNETDB.MDF\";Integrated Security=True"))

【讨论】:

    【解决方案3】:

    正如其他人指出的那样,您需要转义连接字符串中的特殊字符。

    实际上,对于 asp.net,许多开发人员只需将连接字符串放在他们的 web.config 中,然后以后以这种方式访问​​它,如果它发生变化,您只需在一个地方进行更改:

      <connectionStrings>
        <add name="MyConnection" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\Will\Documents\Visual Studio 2015\WebSites\inshort\App_Data\ASPNETDB.MDF;Integrated Security=True/>
      </connectionStrings>
    

    然后使用:

    private string GetConnectionString()
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
    }
    

    【讨论】:

    • 您的配置中缺少右双引号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    相关资源
    最近更新 更多