更新 table mysql 使用 cookie 名称创建

Update table mysql create with cookie name

我在 c# 中的项目创建了一个临时的 table 用于单用户身份验证,并记住了用户名和 cookie。

现在我需要更新这个临时 table 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();
        }
    }
}

你的

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

部分仍然需要是逐字字符串文字,因为它是多行字符串。改成这样;

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

而且我认为您的 table 名称不需要使用重音字符,因为它不是关键字或其他东西。

顺便说一下,由于您将 table 作为输入,我强烈建议您在将其放入 sql 或创建白名单之前进行严格的验证。但是,您应该始终使用 parameterized queries. This kind of string concatenations are open for SQL Injection 攻击。