LINQ查询自动完成文本框 [英] LINQ query for autocomplete textbox

查看:67
本文介绍了LINQ查询自动完成文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用LINQ执行自动完成文本框。我正在尝试将此模块更改为LINQ。但我无法理解从哪里开始。

I am trying to do autocomplete textbox using LINQ. I am trying to change this module to LINQ. But I can not understand from where to start.

<pre>private void Form1_Load(object sender, EventArgs e)
{
	textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
	textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
	AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
	getData(DataCollection);
	textBox1.AutoCompleteCustomSource = DataCollection;
}
private void getData(AutoCompleteStringCollection dataCollection)
{
	string connetionString = null;
	SqlConnection connection ;
	SqlCommand command ;
	SqlDataAdapter adapter = new SqlDataAdapter();
	DataSet ds = new DataSet();
	connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;password=zen412";
	string sql = "SELECT DISTINCT [fname] FROM [employee]";
	connection = new SqlConnection(connetionString);
	try
	{
		connection.Open();
		command = new SqlCommand(sql, connection);
		adapter.SelectCommand = command;
		adapter.Fill(ds);
		adapter.Dispose();
		command.Dispose();
		connection.Close();
		foreach (DataRow row in ds.Tables[0].Rows)
		{
			dataCollection.Add(row[0].ToString());
		}
	}
	catch (Exception ex)
	{
		MessageBox.Show("Can not open connection ! ");
	}
}





我正在尝试对我的表客户端在字段client_name?请指导我。

推荐答案

我认为这就是你所追求的。



注意我的用法使用块以避免必须处理连接和适配器。我还删除了不必要的变量。

我还更改了 getData 以实际返回 AutoCompleteStringCollection 进一步简化代码。

I think this is what you are after.

Note my use of using blocks to avoid having to dispose of the connection and adapter. I've also removed unnecessary variables.
I've also changed the getData to actually return the AutoCompleteStringCollection to simplify the code even further.
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
        textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
        textBox1.AutoCompleteCustomSource = GetData();
    }
    private static AutoCompleteStringCollection GetData()
    {
        const string connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;password=zen412";
        const string sql = "SELECT DISTINCT [fname] FROM [employee]";
        var ds = new DataSet();

        try
        {
            using (var connection = new SqlConnection(connetionString))
            {
                connection.Open();
                using (var adapter = new SqlDataAdapter())
                {
                    adapter.SelectCommand = new SqlCommand(sql, connection);
                    adapter.Fill(ds);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format("Can not open connection ! {0}", ex));
        }

        var dataCollection = new AutoCompleteStringCollection();
        dataCollection.AddRange(ds.Tables[0].AsEnumerable()
                    .Select(x => x.Field<String>("fname"))
                    .ToArray());
        return dataCollection;
    }
}


这篇关于LINQ查询自动完成文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆