c# - Mysql语法错误

标签 c# mysql mysql-error-1064

我收到以下错误,但找不到问题所在:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''String', 'String', '6', 'String', 'String', 'String', 'The' at line 1

C#代码:

    protected void updateDB(byte[] content, int size, string mime)
{
    string school = DropDownList_School.Text;
    string subject = DropDownList_Subject.Text;
    string teacher = DropDownList_Teacher.Text;
    string course = DropDownList_Class.Text;
    string unit = TextBox_Unit.Text;
    string name = TextBox_Name.Text;
    string date = DateTime.Today.ToString();
    string username = System.Web.Security.Membership.GetUser().UserName;
    MySqlConnection connection = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MySqlConnection"].ConnectionString);
    connection.Open();
    MySqlCommand cmd = new MySqlCommand();//(query, connection);
    cmd.Connection = connection;
    cmd.CommandText = "INSERT INTO uploads 
                       (@School, @Subject, @Teacher, @Course, @Unit, @Name, @Username, @Size, @MIME, @content) ";
    cmd.Prepare();
    cmd.Parameters.AddWithValue("@School", school);
    cmd.Parameters.AddWithValue("@Subject", subject);
    cmd.Parameters.AddWithValue("@Teacher", teacher);
    cmd.Parameters.AddWithValue("@Course", course);
    cmd.Parameters.AddWithValue("@Unit", unit);
    cmd.Parameters.AddWithValue("@Name", name);
    cmd.Parameters.AddWithValue("@Username", username);
    cmd.Parameters.AddWithValue("@Size", size);
    cmd.Parameters.AddWithValue("@MIME", mime);
    cmd.Parameters.AddWithValue("@content", content);
    cmd.ExecuteNonQuery();
    connection.Close();
}

ConnectionString 是正确的。

最佳答案

应该是 INSERT INTO uploads VALUES(...) 您可能还想指定列顺序,例如 INSERT INTO uploads(col1, col2, etc...) VALUES(val for col1, val for col2..., etc) 因为如果没有则SQL 会尝试“猜测”,它可能会猜错。

关于c# - Mysql语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6687899/

相关文章:

MySQL : writing query for relational tables for strict categories

mysql - 执行插入/删除批处理 spring-jdbc/mysql 的最佳方法

mysql - SQL 错误 1064 - MySQL 创建 View 时出现问题

php - 有人看到这有什么问题吗? Mysql+PHP错误

c# - HttpClient 下载文件 OutOfMemory 错误

c# - 如何验证 WPF 客户端对 ASP .NET WebAPI 2 的请求

mysql - sql获取上个月的所有查询

mysql-error-1064 - MySQL : error on El Capitan OS after Yosemite OS update

c# - 使用 Streamreader.Read block /RedirectStandardOutput

c# - 如何在周末在 C# 中分配唯一的员工