【问题标题】:AutoComplete with MS Access database使用 MS Access 数据库自动完成
【发布时间】:2018-07-20 04:12:44
【问题描述】:

我想让我的 txtSearch 文本框在 Visual Studio 中自动完成。 找了很久,有一些解释和教程, 但大多数是关于 SQL 数据库的。我的应用程序中有一个 MS Access 数据库。

我想从我的数据库 (appData/Film) 中的标题冒号生成自动完成建议

首先,文本框可以完成这项工作,还是富文本框是完成这项工作所必需的? 我不希望你提供代码,但也许是解释或教程,你知道吗?

谢谢。哎呀。重要信息:我的应用程序基于 C#,并在 Visual Studio 中编码。

【问题讨论】:

  • 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是无关紧要的,因为它们往往会吸引固执己见的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决该问题所做的工作。
  • 好吧,我已将文本框设置为“AutoCompleteMode = SuggestAppend”和“AutoCompleteSource = CustomSource”。

标签: c# visual-studio search autocomplete


【解决方案1】:

试试这个方法。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;

namespace WinAutoComplete
{
    public partial class Form1 : Form
    {
        AutoCompleteStringCollection ProductList = new
        AutoCompleteStringCollection();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //declare connection string
            string cnString = @"Data Source=EXCEL-PC\SQLEXPRESS; Initial Catalog=NORTHWND;" +
                    "Trusted_Connection = True";

            /*use following if you use standard security
            string cnString = @"Data Source=(local);Initial
            Catalog=northwind; Integrated Security=SSPI"; */
            //declare Connection, command and other related objects

            SqlConnection conGetData = new SqlConnection(cnString);
            SqlCommand cmdGetData = new SqlCommand();
            SqlDataReader drGetData;

            try
            {
                //open connection
                conGetData.Open();
                //prepare connection object to get the data through
                //reader and populate into dataset
                cmdGetData.CommandType = CommandType.Text;
                cmdGetData.Connection = conGetData;
                cmdGetData.CommandText = "Select ProductName From Products";
                //read data from command object

                drGetData = cmdGetData.ExecuteReader();
                if (drGetData.HasRows == true)
                {
                    while (drGetData.Read())
                        ProductList.Add(drGetData["ProductName"].ToString());
                }
                else

                    MessageBox.Show("No data found in Products tables");

                //close reader and connection
                drGetData.Close();
                conGetData.Close();
                //set the default pattern to SuggestAppend
                //comboBoxPattern.SelectedIndex = 1;
                txtProductID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                txtProductID.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtProductID.AutoCompleteCustomSource = ProductList;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //check if connection is still open then attempt to close it
                if (conGetData.State == ConnectionState.Open)
                {
                    conGetData.Close();
                }
            }
        }

        private void comboBoxPattern_SelectedIndexChanged(object sender, EventArgs e)
        {

            //switch (comboBoxPattern.Text)
            //{
            //    case "Suggest":
            //        txtProductID.AutoCompleteMode = AutoCompleteMode.Suggest;
            //        break;
            //    case "Append":
            //        txtProductID.AutoCompleteMode = AutoCompleteMode.Append;
            //        break;
            //    case "SuggestAppend":
            //        txtProductID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //        break;
            //}
        }
    }
}

这绝对有效,您可以在下面的屏幕截图中看到。

另外,有机会的时候看看这个。

https://www.connectionstrings.com/

【讨论】:

  • 对不起,我现在才看到你的消息。我去看看!,然后回复!谢谢! :)
  • 你好,现在我已经尝试让它工作了好几个小时,但无法让它工作:(
  • 好的,我刚刚更新了我的帖子。复制/粘贴代码并进行必要的更改(服务器名称、表名称、字段名称等)。这肯定会奏效。
  • 是的,现在可以使用了!非常感谢!但是有一个小问题,当我按下回车键,完成搜索,并让 DataGridView 找到电影时,什么也没有发生。在我插入之前,我的搜索显示在我的 DataGridView 中。所以我差不多完成了,但是这个小东西,你能告诉我如何解决它吗?
  • 我自己修复了最后一个问题!感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-09
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 2014-02-03
相关资源
最近更新 更多