我試着做以下事情:無法添加圖片到數據庫
- 添加新的圖片到數據庫(到一個名爲「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