查询适用于Access,但不适用于我的程序。为什么?

时间:2013-03-09 03:32:56

标签: c# sql ms-access

我在我的应用程序中设置了此查询,以便搜索我的数据库。我把这个查询放入Access,它工作正常。但是,当我把它放入我的程序时,该表有0个条目。你能帮忙吗?

private async Task FilterDB()
        {
            List<string> Filter = new List<string>();

            if (CardNameCheck.IsChecked == true)
                Filter.Add("*" + CardNameBox.Text + "*");
            else
                Filter.Add("*");

            if (CardExpanCheck.IsChecked == true)
                Filter.Add("*" + CardExpanBox.Text + "*");
            else
                Filter.Add("*");

                OleDbConnection DBCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Properties.Settings.Default.DatabaseLocation);
                await DBCon.OpenAsync();

                OleDbDataAdapter CardDA = new OleDbDataAdapter("SELECT * FROM Cards WHERE Name like '" + Filter[0] + "' and Expansion like '" + Filter[1] + "'", DBCon);
                DataSet CardDS = new DataSet();
                CardDA.Fill(CardDS);
                DBCon.Close();

1 个答案:

答案 0 :(得分:0)

我尝试了你的代码并对其进行了一些修改。适用于Access2003 .mdb格式的我。

OleDbConnection DBCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=(k:\mydatabases\mydatabase.mdb");
DBCon.Open();  

// Create a select Command - you need System.Data.OleDb and System.Data for this

OleDbCommand selectCommand = new OleDbCommand();
// define the CommandText with two parameters @Filter1 and @Filter2
selectCommand.CommandText = "SELECT * FROM Cards WHERE Name like @Filter1 and Expansion like @Filter2";

selectCommand.Connection = DBCon;

// Create two string / VarChar Parameters - 
// the following is a standard I commonly use 
// for string/varchar; you might also use OleDbType.NVarChar
OleDbParameter param01 = new OleDbParameter();
param01.ParameterName = "Filter1";
param01.DbType = DbType.AnsiString;
param01.OleDbType = OleDbType.VarChar;
param01.SourceVersion = DataRowVersion.Current;
param01.SourceColumn = "Name";
// provide them with values - I used text boxes for input
// use '%' for like statement - if no parameter provided use single '%' only
if (txtFilter1.Text.ToString().Equals(""))
{
    param01.Value = '%';
}
else
{ 
    param01.Value = '%' + txtFilter1.Text.ToString() + '%'; 
}
// add the parameter to the SelectCommand
selectCommand.Parameters.Add(param01);

// same goes for the second parameter
OleDbParameter param02 = new OleDbParameter();
param02.ParameterName = "Filter2";
param02.DbType = DbType.AnsiString;
param02.OleDbType = OleDbType.VarChar;
param02.SourceVersion = DataRowVersion.Current;
param02.SourceColumn = "Expansion";
if (txtFilter2.Text.ToString().Equals(""))
{
    param02.Value = '%';
}
    else
{
    param02.Value = '%' + txtFilter2.Text.ToString() + '%';
}
selectCommand.Parameters.Add(param02);

OleDbDataAdapter CardDA = new OleDbDataAdapter();
// tell the DataAdapter to use a SelectCommand
CardDA.SelectCommand = selectCommand;
CardDA.GetFillParameters();   // actually not sure if you need this but does no harm either
DataSet CardDS = new DataSet();
CardDA.Fill(CardDS, "TargetTable");
DBCon.Close();

foreach(DataRow row in CardDS.Tables["TargetTable"].Rows)
{
    // do something ;
}
祝你好运!