2013-10-07 98 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