您好我试图保存我的数据库中的所有数据,包括图像。 我的图像的数据类型是longblob我的代码错了吗?请原谅我只是一个初学者,我希望有人能够帮助我,谢谢你。
try {
string myConnection = "datasource=localhost; port=3306; username=root;password=admin; database=dbstudents;";
MySqlConnection myConn = new MySqlConnection(myConnection);
//Images Conversion : Datatype in my database is LONGBLOB
byte[] img = null;
FileStream fs = new FileStream(imgLoc, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
img = br.ReadBytes((int)fs.Length);
// this is a sample query for update statement and update
string Query = "UPDATE tblstudent RIGHT JOIN tbltransact ON tblstudent.StudentID = tbltransact.EnrolleeID SET FirstName=@firstName , LastName=@lastName , MiddleName=@middleName ,ContactNo=@contactNo ,Address=@address ,Age=@age ,Guardian=@guardian,studentPic=@studentPic,PendingBalance=@pendingBalance, WHERE StudentID=@studentID";
MySqlCommand cin = new MySqlCommand(Query, myConn);
myConn.Open();
if (cin.ExecuteNonQuery() == 1)
{
cin.Parameters.AddWithValue("@studentID", int.Parse(search.Text));
cin.Parameters.AddWithValue("@firstName", firstname.Text);
cin.Parameters.AddWithValue("@lastName", lastname.Text);
cin.Parameters.AddWithValue("@middleName", middlename.Text);
cin.Parameters.AddWithValue("@contactNo", ContactNum.Text);
cin.Parameters.AddWithValue("@address", address.Text);
cin.Parameters.AddWithValue("@age", int.Parse(age.Text));
cin.Parameters.AddWithValue("@guardian", guardian.Text);
cin.Parameters.AddWithValue("@studentPic", img );
cin.Parameters.AddWithValue("@pendingBalance", int.Parse(Balance.Text));
myConn.Close();
}
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
这就是我所做的一切,也是我的数据库
答案 0 :(得分:0)
您在执行命令后添加参数。那永远不会奏效。在您拨打ExecuteNonQuery()
时,尚未填写任何参数。
您还要打开文件流。使用File.ReadAllBytes(path)
可以避免手动处理流以读取完整文件的内容。
try
{
String myConnection = "datasource=localhost; port=3306; username=root;password=admin; database=dbstudents;";
//Images Conversion : Datatype in my database is LONGBLOB
Byte[] img = File.ReadAllbytes(imgLoc);
// this is a sample query for update statement and update
String Query = "UPDATE tblstudent RIGHT JOIN tbltransact ON tblstudent.StudentID = tbltransact.EnrolleeID SET FirstName=@firstName , LastName=@lastName , MiddleName=@middleName ,ContactNo=@contactNo ,Address=@address ,Age=@age ,Guardian=@guardian,studentPic=@studentPic,PendingBalance=@pendingBalance, WHERE StudentID=@studentID";
MySqlCommand cin = new MySqlCommand(Query, myConn);
using (MySqlConnection myConn = new MySqlConnection(myConnection))
{
myConn.Open();
cin.Parameters.AddWithValue("@studentID", Int32.Parse(search.Text));
cin.Parameters.AddWithValue("@firstName", firstname.Text);
cin.Parameters.AddWithValue("@lastName", lastname.Text);
cin.Parameters.AddWithValue("@middleName", middlename.Text);
cin.Parameters.AddWithValue("@contactNo", ContactNum.Text);
cin.Parameters.AddWithValue("@address", address.Text);
cin.Parameters.AddWithValue("@age", Int32.Parse(age.Text));
cin.Parameters.AddWithValue("@guardian", guardian.Text);
cin.Parameters.AddWithValue("@studentPic", img );
cin.Parameters.AddWithValue("@pendingBalance", Int32.Parse(Balance.Text));
if (cin.ExecuteNonQuery() == 0)
{
// Handle query failure here
MessageBox.Show(String.Format("No patient found to update for ID {0}", search.Text));
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}