Multiple record select from Access
I have Access table as described below, now in search i have a text box used for search, it take input as string, so when i put in search suppose bmw then i get the search result correctly, but if i put bmw suzuki then i get nothing.
i am using this code to get the values from the databse:
string [] car_name;
string allnames = "'" + String.Join("','", car_name) + "'";
for (int k = 0; k < car_name.Length; k++)
{
oledbcommand cmd1= new oledbcommand("select * from table1 where name in (" + allnames + ",myconnection)
cmd1.exectuenonquery();
开发者_JAVA技巧
this is the table structure:
id name keywords
1 cars bmw
2 cars toyota
3 cars suzuki
4 truck bmw
5 truck scania
6 jeeps bmw
7 jeeps suzuki
It looks like you may be missing the closing paren - ) - from your SQL statement:
oledbcommand cmd1= new oledbcommand("select * from table1 where name in (" + allnames + ")",myconnection)
Your SQL looks fine, but why are you calling ExecuteNonQuery? That is for inserts, updates, and deletes; it won't return any row data. You need to call ExecuteReader or switch to a data adapter and fill a dataset.
Also make sure your IN statement is properly quoted. It should be IN ('value1','value2')
select * from table1 where name in ('BMW','Audi')
should be what gets set as the command text.
I would use OR statment... Like here:
SELECT * FROM Persons
WHERE FirstName='Tove'
OR FirstName='Ola'
精彩评论