【发布时间】:2016-01-17 01:19:33
【问题描述】:
我有一个包含 3 列的数据库:FIRST_NAME、LAST_NAME 和 IMAGE。我总是收到错误“无效的列名'第一列的名称'。”我应该写下名字并单击一个按钮以显示姓氏和图像。我正在使用 C#,这是我当前的代码:
private void button_show_Click(object sender, EventArgs e)
{
try
{
string sql = "select LAST_NAME,IMAGE from Table_1 where FIRST_NAME=" + this.firstname_textbox.Text + "";
if (conn.State != ConnectionState.Open)
conn.Open();
command = new SqlCommand(sql, conn);
SqlDataReader reader = command.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
lastname_textbox.Text = reader[0].ToString();
byte[] img = (byte[])(reader[1]);
if (img == null)
pictureBox1.Image = null;
else
{
MemoryStream ms = new MemoryStream(img);
pictureBox1.Image = Image.FromStream(ms);
}
}
else
{
MessageBox.Show("This Name Does Not Exist");
}
conn.Close();
}
catch(Exception ex)
{
conn.Close();
MessageBox.Show(ex.Message);
}
}
}
谢谢。
【问题讨论】:
-
警告!永远不要从输入字段连接您的查询,因为您很容易受到SQL Injection attack 的攻击。改用参数化查询
标签: c# sql database data-retrieval