使用Excel作为数据库的C#自动完成文本框?

时间:2019-02-19 19:48:50

标签: c# excel visual-studio autocomplete textbox

因此,我正在创建一个具有自动完成文本框的Windows窗体应用程序。我希望文本框从Excel电子表格中提取数据。我将如何去做呢?我注意到您不能将这些项目从多维度转移到文本框中,但是,这是我知道如何从excel提取数据的唯一方法。有什么建议么?

1 个答案:

答案 0 :(得分:0)

我不确定您将如何使用Excel作为后端来执行此操作,或者您为什么甚至想要执行此操作,但这并不适用于普通数据库。

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=Your_Server_Name; Initial Catalog=Your_DB_Name;" +
                    "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;
            //}
        }

    }
}

enter image description here