SQL连接字符串C#ASP NET

时间:2022-06-28 01:41:36

So I'm working on making a submit content page that will store the users input into a database, here is the code I have so far:

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

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.";
        }
    }

However, I am getting an error saying that the connection string contains an invalid character "\", which makes sense but whenever I go to my database's properties and look at the Connection String property, that is what it says.
I am using Microsoft Sql Server Express to have the database locally hosted if that changes anything. Anyone know how to format these connection strings?

但是,我收到一个错误,说连接字符串包含一个无效的字符“\”,这是有道理的,但每当我转到我的数据库的属性并查看连接字符串属性,这就是它所说的。我正在使用Microsoft Sql Server Express在本地托管数据库,如果它发生任何变化。有谁知道如何格式化这些连接字符串?

3 个解决方案

#1


2  

Look at the syntax highlighting. You're trying to put un-escaped double-quotes in a string, which clearly confuses the parser because it thinks your string is terminating earlier and just full of syntax errors after it.

看一下语法高亮。你试图将未转义的双引号放在一个字符串中,这显然会使解析器混淆,因为它认为你的字符串在之前就已经终止并且在它之后只是语法错误。

To escape quotes in a verbatim string literal (one prepended with an @), you need to "double"-double quote them. Like this:

要在逐字字符串文字中删除引号(一个前缀为@),您需要“加倍” - 双引号。像这样:

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

#2


2  

You must escape certain characters in a C# string. If you take away the "literal" string character (@), it will look like this:

您必须转义C#字符串中的某些字符。如果你拿走“文字”字符串字符(@),它将如下所示:

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


1  

As others have pointed out, you need to escape the special characters in your connection string.

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

In practice, with asp.net many developers simply place the connection string in their web.config and then access it later this way if it ever changes you only have to change it in one place:

在实践中,使用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>

And then access it using:

然后使用以下方式访问它:

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

#1


2  

Look at the syntax highlighting. You're trying to put un-escaped double-quotes in a string, which clearly confuses the parser because it thinks your string is terminating earlier and just full of syntax errors after it.

看一下语法高亮。你试图将未转义的双引号放在一个字符串中,这显然会使解析器混淆,因为它认为你的字符串在之前就已经终止并且在它之后只是语法错误。

To escape quotes in a verbatim string literal (one prepended with an @), you need to "double"-double quote them. Like this:

要在逐字字符串文字中删除引号(一个前缀为@),您需要“加倍” - 双引号。像这样:

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

#2


2  

You must escape certain characters in a C# string. If you take away the "literal" string character (@), it will look like this:

您必须转义C#字符串中的某些字符。如果你拿走“文字”字符串字符(@),它将如下所示:

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


1  

As others have pointed out, you need to escape the special characters in your connection string.

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

In practice, with asp.net many developers simply place the connection string in their web.config and then access it later this way if it ever changes you only have to change it in one place:

在实践中,使用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>

And then access it using:

然后使用以下方式访问它:

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