我有我從我看的教程中複製的代碼,我們的代碼在教程中非常相似。C#參數無效SQL
當演示者運行代碼時,它運行正常,但是當我嘗試運行與本教程中相同的代碼時,出現「參數無效」錯誤。
請幫幫
private void Viewbutton_Click(object sender, EventArgs e)
{
conection.Open();
string sqlQuery = "select studnum, course, f_name, l_name, color_image from table3 where studnum='" + textBox1.Text + "'";
cmd = new SqlCommand(sqlQuery, conection);
SqlDataReader dataread = cmd.ExecuteReader();
dataread.Read();
if (dataread.HasRows)
{
lblstudnum.Text = dataread[0].ToString();
lblcourse.Text = dataread[1].ToString();
lblfname.Text = dataread[2].ToString();
lbllname.Text = dataread[3].ToString();
byte[] images = (byte[])dataread[4];
if(images==null)
{
pictureBox1.Image = null;
}
else
{
MemoryStream mstreem = new MemoryStream(images);
pictureBox1.Image = Image.FromStream(mstreem);
}
}
else
{
MessageBox.Show("this data not available");
}
}
誤差線是
pictureBox1.Image = Image.FromStream(mstreem);
您應該在'WHERE'子句中使用參數化查詢而不是連接。 –
哪個教程建議使用字符串連接來構建sql查詢?使用參數化查詢。 –
我發現本教程中的代碼https://www.youtube.com/watch?v=d7klnhcFBEg –