c# - 使用 cookie 名称更新表 mysql create

标签 c# mysql cookies

我的 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/

相关文章:

c# - 如何处理System.Windows.Forms.Axhost+InvalidActiveXState异常

php - 将功能更改为 PDO

mysql - HBase行键设计: matching either of two columns

jquery - 根据选择菜单选项创建 cookie?

Javascript document.cookie 不适用于自定义路径

c# - Asp.net mvc 5 自定义登录无需脚手架,先数据库

c# - 使用 C# Entity Framework 在时态表中插入记录

C# - 一个端口上的多个 TCP 连接?

c# - .NET 应用程序中未调用低级键盘 Hook

mysql - SQL查询速度