[原创]C# Access 模糊查询SQL语句
[原创]C# Access 模糊查询SQL语句由于Access用的是oledb驱动程序,在这里 不能用“*”,必须用“%”。如果你用的是DAO访问Access数据库,则必须用“*”。
!!!注意Access的字段名必须用中括号[]括起来!!!
1.建立OleDbConnection 连接
2. 搜索Access MDB数据库的t_User表

扫一扫加作者微信

!!!注意Access的字段名必须用中括号[]括起来!!!
1.建立OleDbConnection 连接
/// <summary>
/// 创建一个OleDbConnection连接.
/// </summary>
/// <returns></returns>
private System.Data.OleDb.OleDbConnection CreateConnection()
{
string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath;
OleDbConnection conn = new OleDbConnection(connStr);
if (conn.State != ConnectionState.Connecting) conn.Open();
return conn;
}
/// 创建一个OleDbConnection连接.
/// </summary>
/// <returns></returns>
private System.Data.OleDb.OleDbConnection CreateConnection()
{
string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath;
OleDbConnection conn = new OleDbConnection(connStr);
if (conn.State != ConnectionState.Connecting) conn.Open();
return conn;
}
2. 搜索Access MDB数据库的t_User表
//搜索用户表
public DataTable Search(string userID, string userName)
{
string where = " 1=1 ";
if (userID.Trim() != "")
where = where + " and [UserID]=’" + userID + "’";
if (userName.Trim() != "")
where = where + " and [UserName] like ’%" + userName + "%’"; //模糊查询
string sql = "select * from t_User where " + where;
return DataProvider.Instance.GetDataTable(sql);
}
public DataTable Search(string userID, string userName)
{
string where = " 1=1 ";
if (userID.Trim() != "")
where = where + " and [UserID]=’" + userID + "’";
if (userName.Trim() != "")
where = where + " and [UserName] like ’%" + userName + "%’"; //模糊查询
string sql = "select * from t_User where " + where;
return DataProvider.Instance.GetDataTable(sql);
}

扫一扫加作者微信


版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网