我试着做以下事情:无法添加图片到数据库
- 添加新的图片到数据库(到一个名为“PicProfile”栏)。
- 将路径/位置复制到文本框(名为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);
}
}
什么错误信息,你得到什么?你也不需要插入阅读器。尝试'ExecuteScalar'。 –
你在做什么错?从哪里开始.... – musefan
“空路径名称不合法。” - 这是例外情况。这行taht与异常相关:FileStream fstream = new FileStream(this.image_path_txt.Text,FileMode.Open,FileAccess.Read); – Nizan