2015-07-13 71 views
0

我在c#中的项目为用户名和cookie记忆的单用户身份验证创建临时表。使用cookie名称创建更新表mysql创建

现在我需要更新该临时表与MySQL的cookie的名称创建,但我有此错误:

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(); 
     } 
    } 
} 

回答

0

"` 
set Product = 1 
where ID = {0}" 

部分仍需要逐字字符串中,因为它是一个多字符串。改变它的样子;

string sql = String.Format(@"Update `tbl_" + Request.Cookies["userName"].Value + @"` 
          set Product = 1 
          where ID = {0}", 
          productID); 

而且我不认为你需要使用重音字符的表名,因为它不是一个关键字或东西。

顺便说一句,既然你把你的表作为输入,我强烈建议你在把它放入你的sql或创建一个白名单之前做一个强有力的验证。但是,您应该始终使用parameterized queries。这种字符串连接对于SQL Injection攻击是开放的。