如何在c#中创建搜索字段 [英] how to create a search field in c#

查看:93
本文介绍了如何在c#中创建搜索字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中有一个带有sql的数据库应用程序。我有大约30个领域。我想创建一个搜索字段,我可以从我的数据库中搜索记录。我希望我的搜索字段包括组合框和文本框和按钮。组合框应该包括我的所有字段,这样当我在文本框中输入文本时,它将根据组合框中的字段进行搜索

pLS我需要帮助

i have a database application in c# with sql. i have about 30 fields. i want create a search field where i can search records from my database. i want my search field to be include combobox and textbox and button. the combobox should include all my fields , so that when i enter a text in the textbox it will search based on the field in the combobox
pLS i NEED HELP

推荐答案

你应该使用动态sql查询.. :)
这样的




You should use dynamic sql query for that.. :)
like this,

declare @StrQuery nvarchar(max),@columnName varchar(150),@searchText nvarchar(max)
 select @StrQuery='select * from tableName where '+@columnName+' like ''%' + @searchText+ '%'''
 exec(@StrQuery)





其中 @columnName 已从组合代码中传递代码并且 @searchText 已经从代码后面的文本框传递.. :)



where @columnName has been passed from code behind from combobox and @searchText has been passed from textbox from code behind.. :)


把这个编码形成加载

$ / b


con.Open();

cmd =新的SqlCommand(选择Eid,Eid asc从临时订单中En, con);

cmd.CommandType = CommandType.Text;

dr = cmd.ExecuteReader();

if(dr.HasRows ==真的)

{

while(dr.Read())

name.Add(dr [Eid]。ToString()) ;





con.Close();



textBox4.AutoCompleteMode = AutoCompleteMode.Suggest;

textBox4.AutoCompleteSource = AutoCompleteSource.CustomSource;

textBox4.AutoCompleteCustomSource = name;

}
put this coding to form load


con.Open();
cmd = new SqlCommand("select Eid,Ename from temp order by Eid asc",con);
cmd.CommandType = CommandType.Text;
dr = cmd.ExecuteReader();
if (dr.HasRows == true)
{
while (dr.Read())
name.Add(dr["Eid"].ToString());


con.Close();

textBox4.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox4.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox4.AutoCompleteCustomSource = name;
}


这篇关于如何在c#中创建搜索字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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