[原创]C# Access 模糊查询SQL语句
作者:C/S框架网  发布日期:2011/06/12 18:23:11
[原创]C# Access 模糊查询SQL语句

由于Access用的是oledb驱动程序,在这里 不能用“*”,必须用“%”。如果你用的是DAO访问Access数据库,则必须用“*”。

!!!注意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;
}


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);
}



C/S框架网|原创精神.创造价值.打造精品


扫一扫加作者微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务


上一篇 下一篇