我的 C# 项目创建一个临时表,用于存储用户名和 cookie 的单用户身份验证。
现在我需要用 cookie 名称更新 mysql 创建的这个临时表,但是我有这个错误:
Compiler Error Message: CS1010: Newline in constant
出了什么问题?
下面是我的代码。
private void UpdateProduct(string productID)
{
string sql = String.Format(@"Update `tbl_" + Request.Cookies["userName"].Value + "`
set Product = 1
where ID = {0}",
productID);
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand(sql, cn))
{
cmd.ExecuteNonQuery();
}
}
}
最佳答案
你的
"`
set Product = 1
where ID = {0}"
part 仍然需要是逐字字符串文字,因为它是多行字符串。将其更改为;
string sql = String.Format(@"Update `tbl_" + Request.Cookies["userName"].Value + @"`
set Product = 1
where ID = {0}",
productID);
而且我认为您不需要在表名中使用重音字符,因为它不是关键字或其他东西。
顺便说一句,由于您将表作为输入,因此我强烈建议您在将其放入 sql 或创建白名单之前进行严格的验证。但是,您应该始终使用 parameterized queries 。这种字符串连接对SQL Injection开放。攻击。
关于c# - 使用 cookie 名称更新表 mysql create,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31378991/