C# 使用Access 2017在组合框中自动完成

C# 使用Access 2017在组合框中自动完成,c#,ms-access,combobox,autocomplete,C#,Ms Access,Combobox,Autocomplete,我想使用Access 2017在组合框中进行自动完成。所以我用了这个代码。。。但也有一些错误,如: “当前上下文中不存在名称“da” 请帮我纠正这个错误 private void Autocomplete() { string query; OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|

我想使用Access 2017在组合框中进行自动完成。所以我用了这个代码。。。但也有一些错误,如:

“当前上下文中不存在名称“da”

请帮我纠正这个错误

    private void Autocomplete()
    {
        string query;
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/Neth1.accdb");
        //opening connection
        con.Open();
        try
        {
            //initialize a new instance of sqlcommand
            OleDbCommand cmd = new OleDbCommand();
            //set a connection used by this instance of sqlcommand
            cmd.Connection = con;
            //set the sql statement to execute at the data source
            cmd.CommandText = query;

            OleDbDataAdapter da = new OleDbDataAdapter();
            //set the sql statement or stored procedure to execute at the data source
            da.SelectCommand = cmd;
            //initialize a new instance of DataTable
            DataTable dt = new DataTable();
            //add or resfresh rows in the certain range in the datatable to match those in the data source.
            da.Fill(dt);


            foreach (DataRow r in dt.Rows)
            {
                //getting all rows in the specific field|Column
                var rw = r.Field<string>("IMEI");


                //Set the properties of a combobox to make it auto suggest.
                comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
                comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
                //adding all rows into the combobox
                comboBox1.AutoCompleteCustomSource.Add(rw);

            }
        }
        catch (Exception ex)
        {
            //catching error 
            MessageBox.Show(ex.Message);
        }
        //release all resources used by the component
          da.Dispose(); 
        //clossing connection
        con.Close();
    }
private void Autocomplete()
{
字符串查询;
OleDbConnection con=新的OleDbConnection(@“Provider=Microsoft.ACE.OLEDB.12.0;数据源=| DataDirectory |/Neth1.accdb”);
//开口连接
con.Open();
尝试
{
//初始化sqlcommand的新实例
OleDbCommand cmd=新的OleDbCommand();
//设置此sqlcommand实例使用的连接
cmd.Connection=con;
//设置要在数据源上执行的sql语句
cmd.CommandText=查询;
OleDbDataAdapter da=新的OleDbDataAdapter();
//设置要在数据源上执行的sql语句或存储过程
da.SelectCommand=cmd;
//初始化DataTable的新实例
DataTable dt=新的DataTable();
//在数据表的特定范围内添加或重新刷新行,以匹配数据源中的行。
da.填充(dt);
foreach(数据行r在dt.行中)
{
//获取特定字段|列中的所有行
var rw=r.字段(“IMEI”);
//设置组合框的属性以使其自动建议。
comboBox1.AutoCompleteMode=AutoCompleteMode.Suggest;
comboBox1.AutoCompleteSource=AutoCompleteSource.CustomSource;
//将所有行添加到组合框中
comboBox1.AutoCompleteCustomSource.Add(rw);
}
}
捕获(例外情况除外)
{
//捕捉误差
MessageBox.Show(例如Message);
}
//释放组件使用的所有资源
da.Dispose();
//闭合连接
con.Close();
}

由于您使用的是try/catch块,我建议您包括con.Open() 打开连接也可能失败

在处理oledb对象时,使用它也是一种健康的做法 属于{using blocks},负责处理()您创建的(一次性)对象

您不希望在foreach循环中设置这些控件属性。
使用AddRage填充组合框AutoCompleteCustomSource也更有效,而不是每个cicle添加一个对象

以下是我建议编辑您的代码的方式:

//
comboBox1.AutoCompleteMode=AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource=AutoCompleteSource.CustomSource;
//
string query=“选择某物”;
List rw=新列表();
尝试
{
使用(OleDbConnection con=new-OleDbConnection(@“Provider=Microsoft.ACE.OLEDB.12.0;数据源=| DataDirectory |/Neth1.accdb”))
{
con.Open();
使用(OleDbCommand oCmd=新OleDbCommand(查询,con))
{
使用(OleDbDataReader\u reader=oCmd.ExecuteReader())
{
如果(_reader.HasRows==false){return;}
而(_reader.Read())
{
rw.Add((字符串)u读取器[“IMEI”]);
}
}
}
}
}
捕获(例外情况除外){
MessageBox.Show(例如Message);
}
comboBox1.AutoCompleteCustomSource.Clear();
comboBox1.AutoCompleteCustomSource.AddRange(rw.ToArray());
rw=null;

由于您使用的是try/catch块,我建议您包括con.Open() 打开连接也可能失败

在处理oledb对象时,使用它也是一种健康的做法 属于{using blocks},负责处理()您创建的(一次性)对象

您不希望在foreach循环中设置这些控件属性。
使用AddRage填充组合框AutoCompleteCustomSource也更有效,而不是每个cicle添加一个对象

以下是我建议编辑您的代码的方式:

//
comboBox1.AutoCompleteMode=AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource=AutoCompleteSource.CustomSource;
//
string query=“选择某物”;
List rw=新列表();
尝试
{
使用(OleDbConnection con=new-OleDbConnection(@“Provider=Microsoft.ACE.OLEDB.12.0;数据源=| DataDirectory |/Neth1.accdb”))
{
con.Open();
使用(OleDbCommand oCmd=新OleDbCommand(查询,con))
{
使用(OleDbDataReader\u reader=oCmd.ExecuteReader())
{
如果(_reader.HasRows==false){return;}
而(_reader.Read())
{
rw.Add((字符串)u读取器[“IMEI”]);
}
}
}
}
}
捕获(例外情况除外){
MessageBox.Show(例如Message);
}
comboBox1.AutoCompleteCustomSource.Clear();
comboBox1.AutoCompleteCustomSource.AddRange(rw.ToArray());
rw=null;

您的
OleDbDataAdapter da=new OleDbDataAdapter()在try{}范围内,并且


da.Dispose()超出该范围,请尝试将此行放在try{}范围内或移动
OleDbDataAdapter da=new OleDbDataAdapter()在try{}范围之外。

您的
OleDbDataAdapter da=new OleDbDataAdapter()在try{}范围内,并且

da.Dispose()超出该范围,请尝试将此行放在try{}范围内或移动
OleDbDataAdapter da=new OleDbDataAdapter()在try{}范围之外

//
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
//
string query = "SELECT SomeThing";
List<string> rw = new List<string>();

try
{
    using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/Neth1.accdb"))
    {
        con.Open();
        using (OleDbCommand oCmd = new OleDbCommand(query, con))
        {
            using (OleDbDataReader _reader = oCmd.ExecuteReader())
            {
                if (_reader.HasRows == false) { return; }

                 while (_reader.Read())
                 {
                    rw.Add((string)_reader["IMEI"]);
                 }
            }
        }
    }
}
catch (Exception ex) {
    MessageBox.Show(ex.Message);
}
     comboBox1.AutoCompleteCustomSource.Clear();
     comboBox1.AutoCompleteCustomSource.AddRange(rw.ToArray());
     rw = null;