2011-08-19 133 views
0

我想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"); 
    } 
+0

你有錯誤?什麼不起作用? – Joe

回答

2

有你生成的查詢錯誤。你有一個沒有開頭的圓括號。這條線你會得到:

select * from data where id='sometest') 

這將從SQL Server產生語法錯誤。

試試這個:

SqlCommand com = new SqlCommand("select * from data where id='"+textBox1.Text+"'",con); 
0

你必須在SQL語句中額外的括號。

但更重要的是,你正在爲SQL注入開放自己。解決這個毀滅性的和容易避免的問題是使用參數化查詢。