上回,一个美梦被老娘拍醒,决定继续把觉补回来.
上回说到SqlHelper这个东东,完成了对Sql数据库访问的基本操作的封装.
这回就来完成对Access数据库的操作的基本封装吧,这样小菜就在慢慢的往支持多种数据库靠拢了.
小菜有了SqlHelper的基础,相信应该能够完成AccessHelper
听小组其它成员说过Access数据库将会放在根目录的database文件夹下,命名为access_db.config
(注意喽:access_db.mdb 扩展名.mdb才是Access数据库可打开,修改为config扩展名,可以防止数据库被下载)
那么该数据库的相对路径就是 "~/database/access_db.mdb" 了.
using System;
using System.Web;
using System.Data;
using System.Data.OleDb;
namespace Discuz.Data
{
public class AccessHelper
{
private static string m_connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
HttpContext.Current.Server.MapPath("~/database/access_db.config");
/// <summary>
/// 执行一个OleDbCommand返回一个记录集
/// </summary>
/// <remarks>
/// 例如:
/// OleDbDataReader r = ExecuteReader(CommandType.Text, "select id,name from dnt_forums", null);
/// </remarks>
/// <param name="cmdType">OleDbCommand类型 必须为Sql文本命令</param>
/// <param name="cmdText">OleDbCommand文本 必须为Sql语句</param>
/// <param name="cmdParms">OleDbCommand的参数OleDbParameter</param>
/// <returns>一个包含记录的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReader(CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
{
OleDbCommand cmd = new OleDbCommand();
OleDbConnection conn = new OleDbConnection(m_connString);
try
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParms);
OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch (Exception ex)
{
conn.Close();
throw ex;
}
}
/// <summary>
/// 执行一个OleDbCommand并返回第一条记录的第一列的值
/// </summary>
/// <remarks>
/// 例如:
/// OleDbParameter parm = new OleDbParameter("@fid", OleDbType.Integer, 4);
/// parm.Value = fid;
/// Object obj = ExecuteScalar(CommandType.Text, "select name from dnt_forums where fid=@fid", parm);
/// </remarks>
/// <param name="cmdType">OleDbCommand类型 必须为Sql文本命令</param>
/// <param name="cmdText">OleDbCommand文本 必须为Sql语句</param>
/// <param name="cmdParms">OleDbCommand的参数OleDbParameter</param>
/// <returns>返回第一条记录的第一列的值</returns>
public static object ExecuteScalar(CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
{
OleDbCommand cmd = new OleDbCommand();
using (OleDbConnection conn = new OleDbConnection(m_connString))
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParms);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 执行一个OleDbCommand返回受该OleDbCommand影响的行数
/// </summary>
/// <remarks>
/// 例如:
/// OleDbParameter parm = new OleDbParameter("@fid", OleDbType.Integer, 4);
/// parm.Value = fid;
/// int result = ExecuteNonQuery(CommandType.Text, "delete from dnt_forums where fid=@fid", parm);
/// </remarks>
/// <param name="cmdType">OleDbCommand类型 必须为Sql文本命令</param>
/// <param name="cmdText">OleDbCommand文本 必须为Sql语句</param>
/// <param name="cmdParms">OleDbCommand的参数OleDbParameter</param>
/// <returns>返回受OleDbCommand影响的行数</returns>
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
{
OleDbCommand cmd = new OleDbCommand();
using (OleDbConnection conn = new OleDbConnection(m_connString))
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 准备一个OleDbCommand用来执行
/// </summary>
/// <param name="cmd">OleDbCommand对象</param>
/// <param name="conn">OleDbConnection对象</param>
/// <param name="cmdType">OleDbCommand类型 必须为Sql文本命令</param>
/// <param name="cmdText">OleDbCommand文本 必须为Sql语句</param>
/// <param name="cmdParms">OleDbCommand的参数OleDbParameters</param>
private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, CommandType cmdType, string cmdText, OleDbParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (OleDbParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
}
using System.Web;
using System.Data;
using System.Data.OleDb;
namespace Discuz.Data
{
public class AccessHelper
{
private static string m_connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
HttpContext.Current.Server.MapPath("~/database/access_db.config");
/// <summary>
/// 执行一个OleDbCommand返回一个记录集
/// </summary>
/// <remarks>
/// 例如:
/// OleDbDataReader r = ExecuteReader(CommandType.Text, "select id,name from dnt_forums", null);
/// </remarks>
/// <param name="cmdType">OleDbCommand类型 必须为Sql文本命令</param>
/// <param name="cmdText">OleDbCommand文本 必须为Sql语句</param>
/// <param name="cmdParms">OleDbCommand的参数OleDbParameter</param>
/// <returns>一个包含记录的OleDbDataReader</returns>
public static OleDbDataReader ExecuteReader(CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
{
OleDbCommand cmd = new OleDbCommand();
OleDbConnection conn = new OleDbConnection(m_connString);
try
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParms);
OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch (Exception ex)
{
conn.Close();
throw ex;
}
}
/// <summary>
/// 执行一个OleDbCommand并返回第一条记录的第一列的值
/// </summary>
/// <remarks>
/// 例如:
/// OleDbParameter parm = new OleDbParameter("@fid", OleDbType.Integer, 4);
/// parm.Value = fid;
/// Object obj = ExecuteScalar(CommandType.Text, "select name from dnt_forums where fid=@fid", parm);
/// </remarks>
/// <param name="cmdType">OleDbCommand类型 必须为Sql文本命令</param>
/// <param name="cmdText">OleDbCommand文本 必须为Sql语句</param>
/// <param name="cmdParms">OleDbCommand的参数OleDbParameter</param>
/// <returns>返回第一条记录的第一列的值</returns>
public static object ExecuteScalar(CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
{
OleDbCommand cmd = new OleDbCommand();
using (OleDbConnection conn = new OleDbConnection(m_connString))
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParms);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 执行一个OleDbCommand返回受该OleDbCommand影响的行数
/// </summary>
/// <remarks>
/// 例如:
/// OleDbParameter parm = new OleDbParameter("@fid", OleDbType.Integer, 4);
/// parm.Value = fid;
/// int result = ExecuteNonQuery(CommandType.Text, "delete from dnt_forums where fid=@fid", parm);
/// </remarks>
/// <param name="cmdType">OleDbCommand类型 必须为Sql文本命令</param>
/// <param name="cmdText">OleDbCommand文本 必须为Sql语句</param>
/// <param name="cmdParms">OleDbCommand的参数OleDbParameter</param>
/// <returns>返回受OleDbCommand影响的行数</returns>
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
{
OleDbCommand cmd = new OleDbCommand();
using (OleDbConnection conn = new OleDbConnection(m_connString))
{
PrepareCommand(cmd, conn, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 准备一个OleDbCommand用来执行
/// </summary>
/// <param name="cmd">OleDbCommand对象</param>
/// <param name="conn">OleDbConnection对象</param>
/// <param name="cmdType">OleDbCommand类型 必须为Sql文本命令</param>
/// <param name="cmdText">OleDbCommand文本 必须为Sql语句</param>
/// <param name="cmdParms">OleDbCommand的参数OleDbParameters</param>
private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, CommandType cmdType, string cmdText, OleDbParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (OleDbParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
}
看吧,小菜在SqlHelper的基础上,顺利生产了一个AccessHelper.
先让我们来试试看它好不好用.
和之前的SqlHelper一样,我们先遍历dnt_forums中的fid和name
using System;
using System.Web;
using System.Data;
using System.Data.OleDb;
using Discuz.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string cmdText = "select fid,name from dnt_forums";
using (OleDbDataReader reader = AccessHelper.ExecuteReader(CommandType.Text, cmdText, null))
{
while (reader.Read())
{
Response.Write(reader["fid"]);
Response.Write(reader["name"]);
}
}
}
}
using System.Web;
using System.Data;
using System.Data.OleDb;
using Discuz.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string cmdText = "select fid,name from dnt_forums";
using (OleDbDataReader reader = AccessHelper.ExecuteReader(CommandType.Text, cmdText, null))
{
while (reader.Read())
{
Response.Write(reader["fid"]);
Response.Write(reader["name"]);
}
}
}
}
顺利输出: 1版块1 2版块2
那接着测试 <a href="GetFourmName.aspx?fid=1">取出版块名称</a>
using System;
using System.Web;
using System.Data;
using System.Data.OleDb;
using Discuz.Data;
public partial class GetForumName : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int fid = Convert.ToInt32(Request.Params["fid"]);//获取传入参数fid
string cmdText = "select name from dnt_forums where fid=@fid";
OleDbParameter parm = new OleDbParameter("@fid", OleDbType.Integer, 4);//设置Access参数fid
parm.Value = fid;
object name = AccessHelper.ExecuteScalar(CommandType.Text, cmdText, parm);
Response.Write(name);
}
}
using System.Web;
using System.Data;
using System.Data.OleDb;
using Discuz.Data;
public partial class GetForumName : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int fid = Convert.ToInt32(Request.Params["fid"]);//获取传入参数fid
string cmdText = "select name from dnt_forums where fid=@fid";
OleDbParameter parm = new OleDbParameter("@fid", OleDbType.Integer, 4);//设置Access参数fid
parm.Value = fid;
object name = AccessHelper.ExecuteScalar(CommandType.Text, cmdText, parm);
Response.Write(name);
}
}
同样顺利输出了: 版块1
现在小菜手头上已经有了针对Sql数据库的设计,还有一个针对Access数据库的设计.
那么接下来,看小菜如何利用它们,将其改造成支持多数据库的设计.
下篇见..