2012-07-05 3 views
0

MS Access 테이블에서 데이터를 찾기 위해 검색어를 작성했습니다. 그러나 사용자가 ID와 같은 숫자를 입력하지 않으면 쿼리가 실패합니다.MS Access에서 숫자 및 텍스트를 찾으려면 쿼리

"Select UID, FirstName from tUserInfo where UID = " + UserID + " and FirstName like '%" + txtSearchFirst.Text + "%'" 

숫자가 비어있는 경우 표를 검색하는 방법은 무엇입니까?

+0

예를 들어 MS Access 또는 C#에서 실행하고 있습니까? – Fionnuala

+0

나는 이것을 C#에서 돌리고있다. – kanayabhattad

+0

그런 경우 매개 변수 쿼리를 사용하거나 SQL 주입 문제로 실행해야합니다. – Fionnuala

답변

2
Select UID, FirstName from tUserInfo 
where (UID = " + UserID + " or UID is null) 
and FirstName like '%" + txtSearchFirst.Text + "%' 
0

몇 가지 메모.

thisCommand.CommandText = "SELECT UID, FirstName FROM tUserInfo " + 
    " WHERE FirstName Like '%' & ? & '%' And UID & "" Like ? & '%';"; 

    //Names are irrelevant with OLEDB and MS Access, the order is important 
    thisCommand.Parameters.AddWithValue("@Param", txtSearchFirst.Text); 
    thisCommand.Parameters.AddWithValue("@Param", UserID + "");