此代码中的错误是什么?
问题描述:
我想delplay行中的RichTextBox的此代码中的错误是什么?
private void button1_Click(object sender, EventArgs e) {
SqlConnection con = new SqlConnection("Data Source=MOSTAFA\\SQLEXPRESS;Initial Catalog=company;Integrated Security=True");
SqlCommand com = new SqlCommand("select * from data where id='"+textBox1.Text+"')",con);
con.Open();
SqlDataReader read = com.ExecuteReader();
if (read.Read())
richTextBox1.Text = "id" + read[0].ToString();
else
label3.Text=("The client didn't found");
}
答
有你生成的查询错误。你有一个没有开头的圆括号。这条线你会得到:
select * from data where id='sometest')
这将从SQL Server产生语法错误。
试试这个:
SqlCommand com = new SqlCommand("select * from data where id='"+textBox1.Text+"'",con);
答
你必须在SQL语句中额外的括号。
但更重要的是,你正在为SQL注入开放自己。解决这个毁灭性的和容易避免的问题是使用参数化查询。
你有错误?什么不起作用? – Joe