Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查询在Access中工作,但在我的程序中不工作。为什么?_C#_Sql_Ms Access - Fatal编程技术网

C# 查询在Access中工作,但在我的程序中不工作。为什么?

C# 查询在Access中工作,但在我的程序中不工作。为什么?,c#,sql,ms-access,C#,Sql,Ms Access,我在应用程序中设置了此查询,以便在数据库中进行搜索。我将此查询放入Access,它运行良好。但是,当我将其放入程序时,表中有0个条目。你能帮忙吗 private async Task FilterDB() { List<string> Filter = new List<string>(); if (CardNameCheck.IsChecked == true) Filte

我在应用程序中设置了此查询,以便在数据库中进行搜索。我将此查询放入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();
private async Task FilterDB()
{
列表过滤器=新列表();
if(CardNameCheck.IsChecked==true)
Filter.Add(“*”+CardNameBox.Text+“*”);
其他的
过滤器。添加(“*”);
if(cardepancheck.IsChecked==true)
Filter.Add(“*”+CardExpanBox.Text+“*”);
其他的
过滤器。添加(“*”);
OleDbConnection DBCon=新的OleDbConnection(@“Provider=Microsoft.Jet.OLEDB.4.0;数据源=“+Properties.Settings.Default.DatabaseLocation”);
等待DBCon.OpenAsync();
OleDbDataAdapter CardDA=新的OleDbDataAdapter(“从名为“+”过滤器[0]+”的卡中选择*,扩展名为“+”过滤器[1]+”,DBCon);
数据集CardDS=新数据集();
CardDA.Fill(CardDS);
DBCon.Close();

我尝试了您的代码,并对其进行了一些修改。对于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 ;
}

祝你好运!

你使用的是哪个版本的Access?我正在运行Access 2013Put a
try
catch
查看是否有例外我拿出了一个try catch,一个接一个地浏览它,它说它填充了数据集并到达了for(就在剪贴之后),很好,然后它跳过了,因为CardDS.Tables[0].Rows.Count=0我认为是提供程序,请尝试以下操作:
provider=Microsoft.ACE.OLEDB.12.0;数据源=