【问题标题】:Autocomplete in a ComboBox Using Access 2017使用 Access 2017 在组合框中自动完成
【发布时间】:2018-05-01 21:06:27
【问题描述】:

我想使用 Access 2017 在 ComboBox 中进行自动完成。所以我使用了这段代码...但是有一些错误,例如:

“名称'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();
    }

【问题讨论】:

    标签: c# ms-access combobox autocomplete


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您的 OleDbDataAdapter 在 try 块内声明,它不存在于它之外,因此您会看到错误。
      此处显示的query 字符串为空。

      当您处理 OleDb 对象时,通常的做法是使用 using 块,负责处理您创建的一次性对象。

      您不希望在foreach 循环中设置这些控件属性。
      使用AddRage() 填充 ComboBox AutoCompleteCustomSource 也更有效,而不是逐个添加对象。

      这就是我建议编辑您的代码的方式:

      // You can set these properties in the Form designer
      comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
      comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
      // [...]
      
      string query = "SELECT [Some Field] FROM [Some Table] WHERE [Some Condition]";
      List<string> data = new List<string>();
      
      try {
          using (var con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/Neth1.accdb"))
          {
              con.Open();
              using (var cmd = new OleDbCommand(query, con))  
              using (var reader = cmd.ExecuteReader()) {
                  if (reader.HasRows) {
                      while (reader.Read()) {
                         data.Add((string)reader["IMEI"]);
                      }
                  }
              }
          }
      }
      catch {
          // Notify, log, whatever fits
      }
       comboBox1.AutoCompleteCustomSource.Clear();
       comboBox1.AutoCompleteCustomSource.AddRange(data.ToArray());
       data.Clear();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-07
        相关资源
        最近更新 更多