【发布时间】:2016-05-22 17:46:33
【问题描述】:
private void searchButton_Click(object sender, EventArgs e)
{
string constring = "datasource=localhost;port=3306;Initial Catalog = 'dbcpu'; username = root; password =";
string query = "select * from dbcpu.student_profile where name'" + maskedTextBox1.Text + "'";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(query, conDataBase);
MySqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string Lname = myReader.GetString(myReader.GetOrdinal("Lname"));
textBoxLname.Text = Lname;
byte[] imgg = (byte[])(myReader["Pic"]);
if (imgg == null)
pictureBox1.Image = null;
else
{
MemoryStream mstream = new MemoryStream(imgg);
pictureBox1.Image = System.Drawing.Image.FromStream(mstream);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
【问题讨论】:
-
也许写点东西是个好主意,而不仅仅是粘贴一些随机的代码?
-
您缺少 = 符号
WHERE Name =..更好的是,使用 Parameters.Add 方法 -
您真的不想在用户输入上使用字符串连接来构建查询。谷歌“SQL 注入攻击”很好地解释了原因。这是目前最糟糕的安全反模式之一。
-
已经修复。谢谢顺便说一句。我只是错过了一些要输入的字符。 :D 我只是困了。哈哈。 :D