2013-10-07 99 views
-1

我試着做以下事情:無法添加圖片到數據庫

  1. 添加新的圖片到數據庫(到一個名爲「PicProfile」欄)。
  2. 將路徑/位置複製到文本框(名爲image_path_txt)中。在 此外,我可以添加一個記錄與除圖像外的其他字段。

有人能告訴我我做錯了什麼嗎?

private void button1_Click(object sender, EventArgs e) 
{  
    byte[] imageBT = null; 

    FileStream fstream = new FileStream(this.image_path_txt.Text, FileMode.Open, FileAccess.Read); 

    BinaryReader br = new BinaryReader(fstream); 
    imageBT = br.ReadBytes((int)fstream.Length); 
    string constring = "datasource=localhost;port=3306;username=root;password=amg135468lns"; 
    string Query = "insert into db.newuser (FName,LName,Age,Gender,Phone_No, Mobile_No,City, Street, Street_No,Email,idNewUser,PicProfile)"+ "values('" + this.Fname_txt.Text + "','" + this.Lname_txt.Text + "','"+this.Age_txt.Text+"','"+this.Gender+"','" + this.Phone_txt.Text + "','" + this.Mobile_txt.Text + "','" + this.City_txt.Text + "','" + this.Street_txt.Text + "','" + this.StreetNo_txt.Text + "','" + this.Email_txt + "','"+this.user_no_txt.Text+"',@PicP);"; 

    MySqlConnection conDataBase = new MySqlConnection(constring); 
    MySqlCommand cmdDataBase = new MySqlCommand(Query,conDataBase); 
    MySqlDataReader myReader; 

    try 
    { 
     conDataBase.Open(); 
     cmdDataBase.Parameters.Add(new MySqlParameter("@PicP", imageBT)); 

     myReader = cmdDataBase.ExecuteReader(); 
     MessageBox.Show("Saved"); 
     while (myReader.Read()) 
     { 

     } 

    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 

} 
+0

什麼錯誤信息,你得到什麼?你也不需要插入閱讀器。嘗試'ExecuteScalar'。 –

+0

你在做什麼錯?從哪裏開始.... – musefan

+0

「空路徑名稱不合法。」 - 這是例外情況。這行taht與異常相關:FileStream fstream = new FileStream(this.image_path_txt.Text,FileMode.Open,FileAccess.Read); – Nizan

回答

2

空路徑名稱是不合法的。

如果這是錯誤;這是不言而喻的。你正在提供一個空的路徑。或換句話說,this.image_path_txtText空。


哇。所以讓我們從開始,爲什麼你不能將它添加到數據庫。您不能針對INSERT語句發出ExecuteReader。因此,而不是:

myReader = cmdDataBase.ExecuteReader(); 
MessageBox.Show("Saved"); 
while (myReader.Read()) 
{ 

} 

只是這樣做:

cmdDataBase.ExecuteNonQuery(); 

此外,而不是所有這一切:

byte[] imageBT = null; 

FileStream fstream = new FileStream(
    this.image_path_txt.Text, 
    FileMode.Open, 
    FileAccess.Read); 

BinaryReader br = new BinaryReader(fstream); 
imageBT = br.ReadBytes((int)fstream.Length); 

只是這樣做:

byte[] imageBT = File.ReadAllBytes(this.image_path_txt.Text); 

下一頁,讓我們繼續討論資源管理。你需要在這裏充分利用using聲明:

using (MySqlConnection conDataBase = new MySqlConnection(constring)) 
using (MySqlCommand cmdDataBase = new MySqlCommand(Query,conDataBase)) 
{ 
    // add parameters 

    // execute the statement 
} 

接下來,讓我們繼續以SQL注入攻擊。現在,您正在構建一個對SQL注入開放的查詢,因爲它沒有完全參數化。它應是這樣的:

INSERT INTO tbl (field1, field2, field3) VALUES (@field1, @field2, @field3) 

,然後當你添加的參數,只是這樣做:

cmdDataBase.Parameters.AddWithValue("@field1", txtField1.Text); 
cmdDataBase.Parameters.AddWithValue("@field2", txtField2.Text); 
cmdDataBase.Parameters.AddWithValue("@field3", imageBT); 
+0

+1:很棒。另外:避免硬編碼連接字符串 – musefan

+1

@musefan,謝謝!是的,我想我也會這樣建議。但讓我們把它作爲評論。我不想深入研究那個......:D –

+0

感謝您的回答,我會照你的建議去做。 – Nizan