我正在尝试做以下事情:
有人能告诉我我做错了吗?
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 :(得分:2)
空路径名称不合法。
如果那是错误;这是非常不言自明的。你提供了一条空路。或者,换句话说,Text
的{{1}} 为空。
哇。所以让我们从开始,为什么你不能将它添加到数据库。你不能对this.image_path_txt
语句发出ExecuteReader
。所以,而不是:
INSERT
这样做:
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
接下来,让我们继续SQL注入攻击。现在,您正在构建一个对SQL注入广泛开放的查询,因为它没有完全参数化。它应该是这样的:
using (MySqlConnection conDataBase = new MySqlConnection(constring))
using (MySqlCommand cmdDataBase = new MySqlCommand(Query,conDataBase))
{
// add parameters
// execute the statement
}
然后在添加参数时,只需执行以下操作:
INSERT INTO tbl (field1, field2, field3) VALUES (@field1, @field2, @field3)