经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MS Access » 查看文章
Accesss数据库的DBhelper类(带分页)
来源:cnblogs  作者:天然白  时间:2019/3/5 8:53:37  对本文有异议

首先配置web.config,使配置文件连接access数据库:

<connectionStrings>

<add name="DBConnection" connectionString="Provider=Microsoft.Jet.Oledb.4.0;Data Source=|DataDirectory|#lcng.mdb" />  //数据库在本网站App_Data的文件夹中
</connectionStrings>

下面是DBhelper类:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Reflection;
using System.Security.Cryptography;

public class DBHelper
{
public static string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
//不带参数的执行命令
public static int ExecuteCommand(string safeSql)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand cmd = new OleDbCommand(safeSql, connection);
return cmd.ExecuteNonQuery();
}
}
//带参数的执行命令
public static int ExecuteCommand(string sql, params OleDbParameter[] values)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand cmd = new OleDbCommand(sql, connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
}

public static int GetScalar(string safeSql)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand cmd = new OleDbCommand(safeSql, connection);
return Convert.ToInt32(cmd.ExecuteScalar());
}
}
/// <summary>
/// 得到一个字段的值
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static string GetOneValue(string sql)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
using (OleDbCommand cmd = new OleDbCommand(sql, connection))
{
object obj = cmd.ExecuteScalar();
cmd.Prepare();
return obj != null ? obj.ToString() : string.Empty;
}
}
}

public static int GetScalar(string sql, params OleDbParameter[] values)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand cmd = new OleDbCommand(sql, connection);
cmd.Parameters.AddRange(values);
return Convert.ToInt32(cmd.ExecuteScalar());
}
}

public static OleDbDataReader GetReader(string safeSql)
{
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
OleDbCommand cmd = new OleDbCommand(safeSql, connection);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}

public static OleDbDataReader GetReader(string sql, params OleDbParameter[] values)
{
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
OleDbCommand cmd = new OleDbCommand(sql, connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}

public static DataSet GetDataSet(string safeSql)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
DataSet ds = new DataSet();
OleDbCommand cmd = new OleDbCommand(safeSql, connection);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
return ds;
}
}

public static DataTable GetDataSet(string sql, params OleDbParameter[] values)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
DataSet ds = new DataSet();
OleDbCommand cmd = new OleDbCommand(sql, connection);
cmd.Parameters.AddRange(values);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
}
/// <summary>
/// 分页使用
/// </summary>
/// <param name="query"></param>
/// <param name="passCount"></param>
/// <returns></returns>
private static string recordID(string query, int passCount)
{
using (OleDbConnection m_Conn = new OleDbConnection(connectionString))
{
m_Conn.Open();
OleDbCommand cmd = new OleDbCommand(query, m_Conn);
string result = string.Empty;
using (OleDbDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
if (passCount < 1)
{
result += "," + dr.GetInt32(0);
}
passCount--;
}
}
m_Conn.Close();
m_Conn.Dispose();
return result.Substring(1);
}
}
/// <summary>
/// ACCESS高效分页
/// </summary>
/// <param name="pageIndex">当前页码</param>
/// <param name="pageSize">分页容量</param>
/// <param name="strKey">主键</param>
/// <param name="showString">显示的字段</param>
/// <param name="queryString">查询字符串,支持联合查询</param>
/// <param name="whereString">查询条件,若有条件限制则必须以where 开头</param>
/// <param name="orderString">排序规则</param>
/// <param name="pageCount">传出参数:总页数统计</param>
/// <param name="recordCount">传出参数:总记录统计</param>
/// <returns>装载记录的DataTable</returns>
public static DataTable ExecutePager(int pageIndex, int pageSize, string strKey, string showString, string queryString, string whereString, string orderString, out int pageCount, out int recordCount)
{
if (pageIndex < 1) pageIndex = 1;
if (pageSize < 1) pageSize = 10;
if (string.IsNullOrEmpty(showString)) showString = "*";
if (string.IsNullOrEmpty(orderString)) orderString = strKey + " asc ";
using (OleDbConnection m_Conn = new OleDbConnection(connectionString))
{
m_Conn.Open();
string myVw = string.Format(" ( {0} ) tempVw ", queryString);
string where1 = string.Empty;
if (String.IsNullOrEmpty(whereString))
{
where1 = "";
}
else
{
where1 = whereString.Trim();
if (where1.StartsWith("and", StringComparison.CurrentCultureIgnoreCase))
{
where1 = where1.Substring(3);
}
}
string where2 = String.IsNullOrEmpty(where1) ? "" : "where " + where1;
OleDbCommand cmdCount = new OleDbCommand(string.Format(" select count(*) as recordCount from {0} {1}", myVw, where2), m_Conn);
recordCount = Convert.ToInt32(cmdCount.ExecuteScalar());
if ((recordCount % pageSize) > 0)
pageCount = recordCount / pageSize + 1;
else
pageCount = recordCount / pageSize;
OleDbCommand cmdRecord;
if (pageIndex == 1)//第一页
{
cmdRecord = new OleDbCommand(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageSize, showString, myVw, where2, orderString), m_Conn);
}
else if (pageIndex > pageCount)//超出总页数
{
cmdRecord = new OleDbCommand(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageSize, showString, myVw, "where 1=2", orderString), m_Conn);
}
else
{
int pageLowerBound = pageSize * pageIndex;
int pageUpperBound = pageLowerBound - pageSize;
string recordIDs = recordID(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageLowerBound, strKey, myVw, where2, orderString), pageUpperBound);
cmdRecord = new OleDbCommand(string.Format("select {0} from {1} where {2} in ({3}) order by {4} ", showString, myVw, strKey, recordIDs, orderString), m_Conn);

}
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdRecord);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
m_Conn.Close();
m_Conn.Dispose();
return dt;
}
}
/// <summary>
/// MD5加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string MD5DecryptString(string str)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] md5Source = System.Text.Encoding.UTF8.GetBytes(str);
byte[] md5Out = md5.ComputeHash(md5Source);
return Convert.ToBase64String(md5Out);
}

/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="sInputString">输入字符</param>
/// <param name="sKey">Key</param>
/// <returns>加密结果</returns>
public string DESEncryptString(string sInputString, string sKey)
{
try
{
byte[] data = Encoding.Default.GetBytes(sInputString);
byte[] result;
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //密钥
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //初始化向量
ICryptoTransform desencrypt = DES.CreateEncryptor(); //加密器对象
result = desencrypt.TransformFinalBlock(data, 0, data.Length); //转换指定字节数组的指定区域
return BitConverter.ToString(result);
}
catch (Exception ex)
{
//ex.Message = "DES加密异常";
throw ex;
}
}
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="sInputString">输入字符</param>
/// <param name="sKey">Key</param>
/// <returns>解密结果</returns>
public string DESDecryptString(string sInputString, string sKey)
{
try
{
//将字符串转换为字节数组
string[] sInput = sInputString.Split("-".ToCharArray());
byte[] data = new byte[sInput.Length];
byte[] result;
for (int i = 0; i < sInput.Length; i++)
{
data[i] = byte.Parse(sInput[i], System.Globalization.NumberStyles.HexNumber);
}

DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateDecryptor();
result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return Encoding.Default.GetString(result);
}
catch (Exception ex)
{
//ex.Message = "DES解密异常";
throw ex;
}
}
}

 

原文链接:http://www.cnblogs.com/tianranbai/p/10471986.html

 友情链接: NPS