C# 无法将图片添加到数据库中

C# 无法将图片添加到数据库中,c#,.net,ado.net,C#,.net,Ado.net,我正在尝试做以下事情: 将新图片添加到数据库中名为PicProfile的列中。 将路径/位置复制到名为image\u path\u txt的文本框中。在里面 此外,我可以添加一条记录,其中包含除图像以外的其他字段。 有人能告诉我我做错了什么吗 private void button1_Click(object sender, EventArgs e) { byte[] imageBT = null; FileStream fstream = new FileStrea

我正在尝试做以下事情:

将新图片添加到数据库中名为PicProfile的列中。 将路径/位置复制到名为image\u path\u 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);
     }

}
空路径名不合法

如果这是错误;这是不言自明的。您提供的是一条空路径。或者,换句话说,this.image\u path\u txt的文本为空

哇。所以,让我们从为什么不能将其添加到数据库开始。不能对INSERT语句发出ExecuteReader。因此,不是:

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

}
只要这样做:

cmdDataBase.ExecuteNonQuery();
byte[] imageBT = File.ReadAllBytes(this.image_path_txt.Text);
cmdDataBase.Parameters.AddWithValue("@field1", txtField1.Text);
cmdDataBase.Parameters.AddWithValue("@field2", txtField2.Text);
cmdDataBase.Parameters.AddWithValue("@field3", imageBT);
此外,不是所有这些:

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);
只要这样做:

cmdDataBase.ExecuteNonQuery();
byte[] imageBT = File.ReadAllBytes(this.image_path_txt.Text);
cmdDataBase.Parameters.AddWithValue("@field1", txtField1.Text);
cmdDataBase.Parameters.AddWithValue("@field2", txtField2.Text);
cmdDataBase.Parameters.AddWithValue("@field3", imageBT);
接下来,让我们转到资源管理。您需要在此处利用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.ExecuteNonQuery();
byte[] imageBT = File.ReadAllBytes(this.image_path_txt.Text);
cmdDataBase.Parameters.AddWithValue("@field1", txtField1.Text);
cmdDataBase.Parameters.AddWithValue("@field2", txtField2.Text);
cmdDataBase.Parameters.AddWithValue("@field3", imageBT);

您会收到什么错误消息?此外,插入时不需要读取器。试试ExecuteScalar。你做错了什么?从哪里开始…空路径名不合法。-这是例外。这是与异常相关的taht行:FileStream fstream=newfilestreamthis.image\u path\u txt.Text,FileMode.Open,FileAccess.Read;该错误表明您的文件路径不正确。你能举一个例子来说明你在这本书中的内容吗。image\u path\u txt.Text+1:Very thourough。另外:避免硬编码连接strings@musefan谢谢是的,我想我也会这么建议。但让我们把它作为一个评论。我不想深入研究…:谢谢你的回答,我会照你的建议去做。