Search code examples
c#oledbdataadapter

how to select from a table in access with a string variable


I have a database in ms-access and wanted to select from it,

I know my question is very simple but I couldn't find the solution for it

this is how I want to select:

public static void SearchRationCreatorName(string RationCreator)
    {
        string StrCon = System.Configuration.ConfigurationManager....
        OleDbConnection Connection = new OleDbConnection(StrCon);
        OleDbDataAdapter DataA = new OleDbDataAdapter
("Select * from tRations where tRations.RationCreator= [RationCreator]", Connection);

        DataTable Dtable = new DataTable();
        DataA.Fill(Dtable);

but instead of selecting one row it select all of records in that table


Solution

  • That didn't show up well in the comment. I think you mean

    "Select * from tRations where RationCreator= '"+RationCreator+"'" 
    

    The way you worded your title suggests you may want to use a string in place of a tablename but your code suggests otherwise. IF you wanted to know how to select from a dynamic table, let me know.

    Also, this will select all rows that match rationcreator. If you only want one row, use:

    "Select TOP 1 * from tRations where RationCreator= '"+RationCreator+"'"
    

    with or without an ORDER BY predicate