2011-08-13 24 views
0

我試圖使用visual studio 2010將一行添加到mysql數據庫表中,直到現在我還沒遇到問題。我不斷收到錯誤:當我使用帶參數的Insert Into語句時,出現致命錯誤

"Fatal error encountered during command execution."

表結構:

delimiter $$ 

CREATE TABLE `magazine_images` (
    `image_id` int(11) NOT NULL AUTO_INCREMENT, 
    `MagazineKey` int(5) DEFAULT NULL, 
    `image_type` varchar(45) NOT NULL, 
    `image` blob, 
    `image_size` varchar(45) NOT NULL, 
    `image_ctgy` varchar(45) NOT NULL, 
    `image_name` varchar(50) NOT NULL, 
    PRIMARY KEY (`image_id`), 
    KEY `image_magazine` (`MagazineKey`) 
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1$$ 

升C查詢:

string image = "INSERT INTO magazine_images (MagazineKey, image, image_type, image_size, image_ctgy, image_name) " + 
        "VALUES(@magkey, @image, @image_type, @image_size, @image_ctgy, @image_name)"; 
      this.cmd = new MySqlCommand(putImage, this.con); 
      this.read = this.cmd.ExecuteReader(); 
      this.read.Read(); 
      this.cmd.Parameters.AddWithValue("@magkey", int.Parse(labelKey.Text)); 
      this.cmd.Parameters.AddWithValue("@image", textBoxImageFile.Text); 
      this.cmd.Parameters.AddWithValue("@image_name", textBoxImageName.Text); 
      this.cmd.Parameters.AddWithValue("@image_type", comboBoxImageType.Text); 
      this.cmd.Parameters.AddWithValue("@image_ctgy", textBoxImageCategory.Text); 
      this.cmd.Parameters.AddWithValue("@image_size", labelImageSize.Text); 
      this.read.Close(); 

回答

1

無論你的代碼是不完整的,或者您正在將您的查詢後參數你執行查詢 - 你必須在查詢執行之前執行查詢。

此外,你甚至沒有使用image變量 - 你正在使用putImage你沒有顯示。第三,你不應該使用ExecuteReader() - 這是爲了檢索,但你正在做一個插入,因爲你需要ExecuteNonQuery()

總結:要麼你有很多複製&粘貼錯誤或您的示例不反映您的真實代碼。

+0

哦哇,我沒有注意到我執行後添加了參數。非常感謝你的支持。是的,當我由於某種原因複製代碼時,我將'putImage'更改爲'image'。我要睡覺。 此外,感謝您告訴我關於ExecuteReader,ExecuteNonQuery方法。我在今年夏天學習了C#,並且還在學習。 –