2013-02-03 52 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個字符的字符串的空間。只要記住它。

相關問題