access怎么进行模糊查询呢?
本人C#学习中,用别人给的例子模仿做了一个查询系统,但是不知道怎么能够改成模糊查询,请高手帮助。公共里写的usingSystem;usingSystem.Collecti...
本人C#学习中,用别人给的例子模仿做了一个查询系统,但是不知道怎么能够改成模糊查询,请高手帮助。公共里写的
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Windows.Forms;
namespace StudentManagerSys
{
class Student
{
public String strCon;
public OleDbConnection conn;
public OleDbCommand cmd;
public Student()
{
strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + System.Windows.Forms.Application.StartupPath + @"\student.mdb";
conn = new OleDbConnection(strCon);
cmd = new OleDbCommand();
}
public DataSet select(string selectype,string item)
{
try
{
cmd.CommandText = "select * from student where " + selectype + "=" + "'" + item + "'";
cmd.Connection = conn;
OleDbDataAdapter dr = new OleDbDataAdapter(cmd.CommandText, conn.ConnectionString);
DataSet ds = new DataSet();
dr.Fill(ds, "infom");
conn.Close();
return ds;
}
catch
{
conn.Close();
MessageBox.Show("查询失败!");
}
return null;
}
public DataSet selectAll()
{
try
{
cmd.CommandText = "select * from student ";
cmd.Connection = conn;
OleDbDataAdapter dr = new OleDbDataAdapter(cmd.CommandText, conn.ConnectionString);
DataSet ds = new DataSet();
dr.Fill(ds, "infom");
conn.Close();
return ds;
}
catch
{
conn.Close();
MessageBox.Show("查询失败!");
}
return null;
}
查询界面下的是
private void button_search_stu_Click(object sender, EventArgs e)
{
string type = comboBox1.Text;
string item = textBox1.Text;
Student stu = new Student();
DataSet dstu = stu.select(type, item);
dataGridView1.DataSource = dstu;
dataGridView1.DataMember = "infom";
}
按网上方法改了半天也不成,请大家帮忙,谢谢了 展开
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Windows.Forms;
namespace StudentManagerSys
{
class Student
{
public String strCon;
public OleDbConnection conn;
public OleDbCommand cmd;
public Student()
{
strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + System.Windows.Forms.Application.StartupPath + @"\student.mdb";
conn = new OleDbConnection(strCon);
cmd = new OleDbCommand();
}
public DataSet select(string selectype,string item)
{
try
{
cmd.CommandText = "select * from student where " + selectype + "=" + "'" + item + "'";
cmd.Connection = conn;
OleDbDataAdapter dr = new OleDbDataAdapter(cmd.CommandText, conn.ConnectionString);
DataSet ds = new DataSet();
dr.Fill(ds, "infom");
conn.Close();
return ds;
}
catch
{
conn.Close();
MessageBox.Show("查询失败!");
}
return null;
}
public DataSet selectAll()
{
try
{
cmd.CommandText = "select * from student ";
cmd.Connection = conn;
OleDbDataAdapter dr = new OleDbDataAdapter(cmd.CommandText, conn.ConnectionString);
DataSet ds = new DataSet();
dr.Fill(ds, "infom");
conn.Close();
return ds;
}
catch
{
conn.Close();
MessageBox.Show("查询失败!");
}
return null;
}
查询界面下的是
private void button_search_stu_Click(object sender, EventArgs e)
{
string type = comboBox1.Text;
string item = textBox1.Text;
Student stu = new Student();
DataSet dstu = stu.select(type, item);
dataGridView1.DataSource = dstu;
dataGridView1.DataMember = "infom";
}
按网上方法改了半天也不成,请大家帮忙,谢谢了 展开
5个回答
展开全部
cmd.CommandText = "select * from student where " + selectype + "=" + "'" + item + "'";
改成cmd.CommandText = "select * from student where " + selectype + "like" + "'*" + item + "*'";
like'%abc%'是SQL里的,ACCESS里用like'*abc*'...
改成cmd.CommandText = "select * from student where " + selectype + "like" + "'*" + item + "*'";
like'%abc%'是SQL里的,ACCESS里用like'*abc*'...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
cmd.CommandText = "select * from student where " + selectype + "=" + "'" + item + "'";
这里改成
cmd.CommandText = "select * from student where " + selectype + "like %" + "'" + item + "'%";
这里改成
cmd.CommandText = "select * from student where " + selectype + "like %" + "'" + item + "'%";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
acccess
模糊查询语句:
"select * from Table where Field like '*" & keyword & "*'"
Table 表 Field 查询的字段
模糊查询语句:
"select * from Table where Field like '*" & keyword & "*'"
Table 表 Field 查询的字段
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
"select * from student where " + selectype + " like " + "'%" + item + "%'";
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用 like
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询