我收到多個參數 - 語法錯誤在字符串中查詢表達式:VS 2010 MS-Access 2003中
OleDBException查詢是的「語法錯誤(缺少操作員)未處理的錯誤
表達「(StudentID = 100'或StudentName ='Nick'或StudentCNCI ='78894452)Bob'。「
private void btnFind_Click(object sender, EventArgs e)
{
string title = textBox1.Text.ToString();
string queryString = "SELECT * FROM Students WHERE (StudentID = " + StudIDTb.Text.ToString() + "' OR StudentName = '" + StudNameTb.Text.ToString() + "' OR StudentCNCI = '" + StudCNCITb.Text.ToString() + ")" + title;
OleDbCommand command = new OleDbCommand();
command.CommandText = queryString;
command.Connection = myCon;
myCon.Open();
OleDbDataReader dr = command.ExecuteReader(); // error pointing here
while (dr.Read())
{
StudIDTb.Text += String.Format("StudentID: {0}\n", dr["StudentID"].ToString());
StudNameTb.Text += String.Format("StudentName: {0}\n", dr["StudentName"].ToString());
StudCNCITb.Text += String.Format("StudentCNIC: {0}\n", dr["StudentCNIC"].ToString());
StudDOBTb.Text += String.Format("StudentDOB: {0}\n", dr["StudentDOB"].ToString());
}
myCon.Close();
}
我也曾嘗試...
string queryString = "SELECT * FROM Students WHERE (StudentID = " + StudIDTb.Text + "' OR StudentName = '" + StudNameTb.Text + "' OR StudentCNCI = '" + StudCNCITb.Text + ")" + title;
我不想給你錯誤的印象,我很「懶」,但我假設我得到這個錯誤,因爲我有不正確地查詢它,或者我犯了一個錯字錯誤,或者它可能是別的。請有人可以幫助我,提前感謝。
ps我知道我越來越批評不使用參數化查詢。一旦我獲得了基本的權利,我會改變它。我知道很多類似的問題都在這裏問過,但我仍然無法理解。
更新1 我已經把它改爲
我現在收到的錯誤...
查詢表達式語法錯誤(逗號)
我正在研究它
更新2
string queryString = "SELECT * FROM Students WHERE StudentID = " + StudIDTb.Text + "' OR StudentName = '" + StudNameTb.Text + "' OR StudentCNCI = '" + StudCNCITb.Text + "'";
接收相同的錯誤。
尋找到它
更新3 如果不能解決我做的事情應該是這樣,使用參數化查詢,強烈建議,如果這意味着要解決的問題,可能容易被發現代碼的任何問題
什麼是「+標題」應該在您的查詢字符串的結尾? – 2013-03-10 15:01:54
錯誤字符串告訴你問題是什麼,「'78894452)Bob'」。在Text屬性上調用ToString根本沒有意義。 – PhoenixReborn 2013-03-10 15:03:42
我刪除了+標題,但在沒有Bob的情況下得到相同的錯誤。 – bucketblast 2013-03-10 15:05:21