2013-02-03 53 views
0

C#代码: //这里我试图在我的表中插入值。我在命令执行过程中遇到致命错误

case true: 
    Connection.Connect(); 
    int fk_pic = IdPic(); 
    string insertWork = "INSERT INTO work (fk_id_pic, fk_login, university, lesson, name, info, date_work, price) VALUES (@fk_pic, @fk_login, @university, @lesson, @name, @info, @date_work, @price); Allow User Variables = true;"; 
    MySqlCommand work = new MySqlCommand(insertWork, Connection.Connect()); 
    work.Parameters.AddWithValue("@fk_pic", fk_pic); 
    work.Parameters.AddWithValue("@fk_login", Program.form1.login); 
    work.Parameters.AddWithValue("@university", textBox1.Text); 
    work.Parameters.AddWithValue("@lesson", textBox2.Text); 
    work.Parameters.AddWithValue("@name", textBox3.Text); 
    work.Parameters.AddWithValue("@info", richTextBox1.Text); 
    work.Parameters.AddWithValue("@date_work", DateTime.Now); 
    work.Parameters.AddWithValue("@ price", textBox4.Text); 
    work.ExecuteNonQuery(); 
    Connection.Disconnect(); 
    break; 

SQL查询:

CREATE TABLE pic 
( 
    id_pic INTEGER UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'id of image', 
    caption CHAR(45) NOT NULL COMMENT 'the name of image', 
    who_add CHAR(30) NOT NULL COMMENT 'who add this image', 
    img LONGBLOB NOT NULL, 
    CONSTRAINT pkId_pic PRIMARY KEY(id_pic) 
) 
COMMENT 'Table for hosting images'; 

不知道该怎么办。等待响应)

+1

什么是错误? –

+0

您可以发布错误的详细信息 - 例如异常消息吗? –

+0

“work.Parameters.AddWithValue(”@ price“,textBox4.Text);” - 可能不应该是“@”后面的空格 –

回答

0

更改名为“name”的列或变量的名称。

名称/名称通常是一个保留字。

这可能是问题所在。

-1

请记住,在mysql中的.NET连接器的参数标识为?,而不是@

随机评论

望着SQL代码我注意到,您使用的char字段听起来更像varchar领域。当您将一列定义为字符时,它将为放入表中的所有条目保留已定义的空间量。

例子:

create table test (
    myvalue char(255) 
) 

不管你只WITE“一”或“世界你好”到myvalue列将保留与磁盘上的255个字符的字符串的空间。只要记住它。

相关问题