C#实现精确查询和模糊查询
来源:互联网 发布:网络教育文凭多少钱 编辑:程序博客网 时间:2024/04/30 15:19
方法一:
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;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
private static string strConnect = "Data Source=.//SQLEXPRESS;database=test;UID=sa;PWD=sa;";
private SqlConnection conn = new SqlConnection(strConnect);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
conn.Open();
string sql = string.Format("SELECT * FROM MANLIAN WHERE 姓名='{0}'or 电话='{0}' or 电子邮件='{0}'", textBox1.Text);
SqlCommand jingzhun = new SqlCommand(sql, conn);
SqlDataReader reader = jingzhun.ExecuteReader();
while (reader.Read())
{
string[] str = new string[3];
str[0] = reader[0].ToString();
str[1] = reader[1].ToString();
str[2] = reader[2].ToString();
ListViewItem li = new ListViewItem();
li.SubItems.Clear();
li.SubItems[0].Text = str[0];
li.SubItems.Add(str[1]);
li.SubItems.Add(str[2]);
LV.Items.Add(li);
reader.Close();
conn.Close();
} MessageBox.Show("没有你要的东西");
}
else if (radioButton2.Checked)
{
conn.Open();
string a = "%" + textBox1.Text + "%";
string sql = string.Format("SELECT * FROM MANLIAN WHERE 姓名 like '{0}' or 电话 like '{0}' or 电子邮件 like '{0}'", a);
SqlCommand mohu = new SqlCommand(sql, conn);
SqlDataReader reader = mohu.ExecuteReader();
while (reader.Read())
{
string[] str = new string[3];
str[0] = reader[0].ToString();
str[1] = reader[1].ToString();
str[2] = reader[2].ToString();
ListViewItem li = new ListViewItem();
li.SubItems.Clear();
li.SubItems[0].Text = str[0];
li.SubItems.Add(str[1]);
li.SubItems.Add(str[2]);
LV.Items.Add(li);
reader.Close();
conn.Close();
}MessageBox.Show("没有你要的东西!");
}
else
{
MessageBox.Show("请选择查询方式!");
}
}
}
}
方法二:public string[] getChaXunstr(string strtemp) { string[] stra= strtemp.Split(' '); string strb=""; for (int i = 0;i<stra.Length;i++) { strb += stra[i]+"&"; } strtemp = strb.Substring(0,strb.Length-1); stra= strtemp.Split(' '); strb=""; for (int i = 0;i<stra.Length;i++) { strb += stra[i]+"&"; } return strb.Substring(0,strb.Length-1).Split('&'); }protected void Button1_Click(object sender, EventArgs e) { SqlConnection cnn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); cmd.Connection = cnn; string strsql = "select * from temp_messi where 1<>1"; string strkey = TextBox1.Text; string[] key = getChaXunstr(strkey); for (int i=0;i<key.Length;i++) { strsql +=" or (name like '%"+key[i]+"%')"; } cmd.CommandText = strsql; SqlDataAdapter adp = new SqlDataAdapter(); adp.SelectCommand = cmd; DataSet ds = new DataSet(); adp.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); }
- C#实现精确查询和模糊查询
- 模糊查询&精确查询
- NSPredicate 模糊、精确、查询
- NSPredicate 模糊、精确、查询
- NSPredicate 模糊、精确、查询
- NSPredicate模糊、精确、查询
- Elasticsearch中同时实现对某一字段的精确查询和模糊查询的方法
- NSPredicate(谓词) 模糊、精确、查询
- iOS NSPredicate 模糊、精确、查询
- linq实现动态查询和模糊查询
- ADO精确查询、模糊查询(经验)
- SpringMVC+Mybatis之获取查询列表和模糊查询、精确查询全过程详解
- C# xml模糊查询的实现
- C# Combobox控件实现模糊查询功能
- Linq的模糊查询(包含精确模糊查询)
- lucene模糊查询 精确查询 包含关键字 不包含关键字 包含全部关键字 范围查询 模糊查询 通配符查询 lucene各种查询实现示例
- C#模糊查询Access
- c# --sql-模糊查询
- static
- X.509 数字证书结构和实例
- 2010-4-22 星期四 晴转多云
- 字符串转换数字
- 新来的
- C#实现精确查询和模糊查询
- SQL中char、varchar、text和nchar、nvarchar、ntext的区别
- C#:String.Format数字格式化输出 {0:N2} {0:D2} {0:C2}...
- 第8章 判断 & 第7章练习
- 指数
- 学生学不好,谁之责?
- 数据库备份的种类
- 类
- 连接数据库