2013-07-30 65 views
0

我想看看是否記錄表中存在它之前,我寫一個文件,但我不斷收到一個錯誤如何查找數據庫記錄是否存在?

string InformationCheck = "Select FName,LName from Customer where FName ='" + this.f_Name.Text + "' and LName = '" + l_Name.Text + "'"; 

OleDbCommand InfoCheck = new OleDbCommand(InformationCheck, conn); 

String strResult = String.Empty; 

try 
{ 

    conn.Open(); 
    strResult = (String)InfoCheck.ExecuteScalar(); 

    if (strResult.Length == 0) 
    { 
     MessageBox.Show("Dose Not Exist"); 
     AddInfo.ExecuteReader(); 
     conn.Close(); 
     this.Hide(); 
     f1.Show(); 
    } 
    else 
    { 
     MessageBox.Show("Exists"); 

    }      
} 
catch (Exception ex) 
{ 
    MessageBox.Show("" + ex + ""); 
} 
+2

你能分享錯誤訊息嗎? – WLin

+3

你在做(字符串)ExecuteScalar()但要求返回2個項目。 – Jonesopolis

+0

直到您指定錯誤消息:FName的結尾單引號和「和」關鍵字之間是否有空格?在上面的文字中很難說。 – JTMon

回答

0

感謝我得到了我的問題,通過改變.ExecuteReader()解決;到.ExecuteNonQuery();

它現在檢查它是否在數據庫中,如果它沒有將信息輸入到表中。

+0

很好聽,你現在有更多的時間來閱讀這個。 http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx,如果時間允許http://www.codeproject.com/Articles/9378/SQL-Injection-Attacks -and-Some-Tips-on-How-to-Prev –

+0

非常感謝你看看這些@philsoady – user2552211

相關問題