comboBox绑定数据库、模糊查询
实现:
一、绑定数据库
点击查询按钮,comboBox显示从数据库查到的某字段的一列数据

方法:在按钮的点击事件绑定数据库
private void button1_Click(object sender, EventArgs e)
{
using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select * from test t");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
//con.Close();
DataTable dt = ds.Tables[];
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember="name"; //要显示的数据库中某字段的一列数据
this.comboBox1.ValueMember = "id"; //设置了ValueMember=‘id’ }
} }
设置了ValueMember=‘id’,然后前台页面就可以根据comboBox选中的项,获取其id,
根据选中的comboBox的项获取其id值:string str=comboBox1.SelectedValue.ToString();
根据选中的comboBox的项获取其文本值:string str=comboBox1.Text;
二、模糊查询(在上面代码的基础上加两句)
在comboBox上输入的字传给str,sql语句模糊查询这个str即可
string str = this.comboBox1.Text;
using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select * from test t where t.name like '%"+str+"%'");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
//con.Close();
DataTable dt = ds.Tables[];
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "name";
this.comboBox1.ValueMember = "id";
this.comboBox1.DroppedDown = true; //点击查询,让comboBox下拉列表展开显示得到的结果,
}
}
三、自动补全后面剩余字段
需要绑定数据源到Load方法里,而且打开页面就能显示所有数据,
private void Form1_Load(object sender, EventArgs e)
{
using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select * from test t");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
//con.Close();
DataTable dt = ds.Tables[];
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "name";
this.comboBox1.ValueMember = "id";
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //自动补全后面剩余字段
this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems; //自动补全后面剩余字段
} } }
comboBox绑定数据库、模糊查询的更多相关文章
- silverlight中 ComboBox绑定数据库,并获取当前选定值
silverlight中 ComboBox绑定数据库,并获取当前选定值 在silverlight中 用combobox下拉菜单绑定数据库的方法和用DataGrid绑定数据库的方法类似. page.xa ...
- 老生常谈combobox和combotree模糊查询
FIRST /** * combobox和combotree模糊查询 * combotree 结果显示两级父节点(手动设置数量) * 键盘上下键选择叶子节点 * 键盘回车键设置文本的值 */ (fun ...
- python 操作mongodb数据库模糊查询
# -*- coding: utf-8 -*-import pymongoimport refrom pymongo import MongoClient #创建连接#10.20.66.106clie ...
- ext.net中ComboBox空间实现模糊查询
ComboBox中的属性添加Mode="Local"可以实现第一个字的模糊查询但是搜索中间的字无法实现 现提供一下方法使用正则表达式实现全模糊查询 <ext:ComboBox ...
- 2019-2-14sql server数据库模糊查询语句
sql server数据库模糊查询语句 确切匹配: select * from hs_user where ID=123 模糊查询 select * from hs_user where ID l ...
- SQL Server数据库————模糊查询和聚合函数
***********模糊查询*********/ 关键字: like (!!!!字符串类型) in (,,) 匹配()内的某个具体值(括号里可以写多个值) between... and.. 在某两 ...
- WPF中ComboBox绑定数据库自动读取产生数据
前台端 <ComboBox HorizontalAlignment="Name="cmb_SSBM" DisplayMemberPath="NAME&qu ...
- easyui combobox实现本地模糊查询
直接上代码 $("#combobox1").combobox({ valueField : "value", textField : "text&qu ...
- combobox实现模糊查询自动填充
利用winform设计软件界面时,经常用到combobox控件,但有时需要绑定数据表中的数据,更进一步,需要实现对数据表中数据的模糊查询功能.本文就讲讲述如何用C#实现combobox下拉列表的模糊查 ...
随机推荐
- jquery实现全选、反选、不选
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8&quo ...
- 【摘抄】meta系列用法总结【持续更新中】
meta标签分两大部分:HTTP标题信息(HTTP-EQUIV)和页面描述信息(NAME). ★页面描述信息NAME变量 name是描述网页的,对应于Content(网页内容),以便于搜索引擎机器人 ...
- Java Date与SimpleDateFormat
最近在弄一些涉及到时间处理的项目.本来自己写了一个时间转换函数,虽然能用但是过于麻烦而且不够规范,于是学习了下java自带的时间处理的类. public class Timechg { public ...
- OpenGL的glTranslatef平移变换函数详解
OpenGL的glTranslatef平移变换函数详解 glTranslated()和glTranslatef()这两个函数是定义一个平移矩阵,该矩阵与当前矩阵相乘,使后续的图形进行平移变换. 我们先 ...
- x+y = ((x&y)<<1) + (x^y) 证明
法一:我们考虑x,y在二进制表示时候,按位相加其中第i位xi+yi = ((xi&yi)<<1) + (xi^yi)其中(xi&yi)<<1表示当xi和yi都是 ...
- java多线程下载网络图片
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader ...
- 一个最简html5文档来说明html5的新特性和写法
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- JBPM工作流入门总结
关于JBPM工作流 1.工作流 工作流是一项分离业务操作和系统流程的技术.工作流由实体(Entity).参与者(Participant).流程定义(Flow Definition).工作流引擎(Eng ...
- base(C# 参考)
原文地址:https://msdn.microsoft.com/zh-cn/library/hfw7t1ce.aspx base 关键字用于从派生类中访问基类的成员: 调用基类上已被其他方法重写的方法 ...
- 获取本机 Android 默认sha1 秘钥
获取本机 Android 默认sha1 秘钥: 以Windows操作系统为例,打开CMD,运行以下指令将得到所有默认秘钥. keytool -list -v -keystore C:\Users\pa ...