user1275351
user1275351

Reputation: 41

Sql statement works in Access but not when I run from C#

The statement:

SELECT [ToWhom] FROM [myChecks] WHERE [ToWhom] like '*e*' 

works fine in Access but when I run it from C# I get back and empty Dataset. Here is the code:

string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Checkbook.accdb";
OleDbConnection Conn = new OleDbConnection();
Conn.ConnectionString = connectionstring;
OleDbCommand myCommand = Conn.CreateCommand();
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();
DataSet myDataset = new DataSet();
DataTable EmptyDataTable = new DataTable();
myCommand.CommandText = SQL;
myDataAdapter.SelectCommand = myCommand;
Conn.Open();
try
{
//This part does not throw an error it just return an empty Dataset
myDataAdapter.Fill(myDataset);
Conn.Close();
return myDataset.Tables[0];
}
catch(SyntaxErrorException e)
{
MessageBox.Show(e.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Conn.Close();
 return EmptyDataTable;
}         

Any help would be appreciated.

Upvotes: 2

Views: 494

Answers (1)

Fionnuala
Fionnuala

Reputation: 91316

In C# with the above connection, use percent (%) as the wildcard, not asterisk (*)

SELECT [ToWhom] FROM [myChecks] WHERE [ToWhom] like '%e%' 

Inside MS Access, in the query design window when ANSI 92 option is not set (that is, the usual set-up), the wildcard is asterisk (*), outside of Access is usually percent (%).

Upvotes: 6

Related Questions