用sql語句對access數據庫進行多條件查詢

在對access數據庫進行數據查詢的時候要注意,where後面的條件要加單引號:

select * from 故障記錄 where 故障現象代碼='F001'


多條件查詢實現的代碼如下:

string sql = "select * from 故障記錄 where";
 if(textBox_machine.Text!="")
 {
      sql += " 機器編號=" + "'" + textBox_machine.Text +"'"+ " and ";
 }
 if (textBox_false.Text != "")
  {
      sql += " 故障現象代碼=" +"'"+ textBox_false.Text +"'"+ " and ";
  }
if (textBox_xian.Text != "")
  {
       sql += " 故障現象=" + "'" + textBox_xian.Text + "'" + " and ";
  }
if (textBox_cause.Text != "")
  {
       sql += " 故障原因代碼=" + "'" + textBox_cause.Text + "'" + " and ";
  }
if (textBox_yuan.Text != "")
   {
        sql += " 故障原因=" + "'" + textBox_yuan.Text + "'" + " and ";
    }
if (textBox_time.Text != "")
    {
        sql += " 時間=" + "'" + textBox_time.Text + "'" + " and ";
    }
sql = sql.Substring(0, sql.Length - 5);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章