asp.net(C#) Access 数据操作类

这里气候宜人,物产丰富,风景优美,是中国重点风景旅游城市。多明媚的秋天哪,这里,再也不是焦土和灰烬,这是千万座山风都披着红毯的旺盛的国土。那满身嵌着弹皮的红松,仍然活着,傲立在高高的山岩上,山谷中汽笛欢腾,白望在稻田里缓缓飞翔。秋天的美是成熟的——它不像春那么羞涩,夏那么坦露,冬那么内向。

using System;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Xml;
using System.Collections;
namespace Website.Command
{
/// <summary>
/// WSplus 的摘要说明。
/// </summary>
public class AccessClass : System.Web.UI.Page
{
private static OleDbConnection sconn = null;
public static string _returnValue = "0";
public WSplus()
{
//
// TODO: 在此处添加构造函数逻辑
//
sconn = new OleDbConnection("Provider="Microsoft.Jet.OLEDB.4.0;Data" Source=" + Server.MapPath("/..db.mdb") + ";");
}
/// <summary>
///开启数据
/// </summary>
private static void connOpen()
{
if (sconn.State="=ConnectionState.Closed)"
try
{
sconn.Open();
}
catch (Exception ex)
{
throw new Exception("数据库初始化错误!!!"+ex.Message);
}
}
/// <summary>
/// 关闭数据库
/// </summary>
public void connClose()
{
if (sconn.State ="=" ConnectionState.Open)
try
{
sconn.Close();
}
catch (Exception ex)
{
throw new Exception("OleDb Close Error!"+ex.Message);
}
}
/// <summary>
/// 存储过程型CMD初始化!
/// </summary>
/// <param name="ht"></param>
/// <param name="sCmd"></param>
/// <param name="proc"></param>
private static void ConfigCMD(Hashtable ht,OleDbCommand sCmd,string sqls)
{
if (sCmd ="=" null) throw new ArgumentException("sCmd未初始化");
if (ht != null && ht.Count != 0)
{
IDictionaryEnumerator items = ht.GetEnumerator();
while (items.MoveNext())
sCmd.Parameters.Add(MakeParam(items.Key.ToString(),items.Value.ToString()));
}
sCmd.Connection = sconn;
sCmd.CommandText = sqls;
}
private static void ConfigCMD(ArrayList alist,OleDbCommand sCmd,string sqls)
{
if (sCmd ="=" null) throw new ArgumentException("sCmd未初始化");
if (alist != null && alist.Count != 0)
{
foreach (object items in alist)
{
Command.Model.typeFiles cmt = (Command.Model.typeFiles)items;
sCmd.Parameters.Add(MakeParam(cmt.Fkey.ToString(),cmt.Fvalue1.ToString(),cmt.Fvalue2.ToString()));
}
}
sCmd.Connection = sconn;
sCmd.CommandText = sqls;
}
private static OleDbParameter MakeParam(string key,string value)
{
return new OleDbParameter(key,value);
}
private static OleDbParameter MakeParam(string key,string F1,string F2)
{
OleDbParameter par = null;
if (F2.ToUpper() ="=" "STRING")
{
par = new OleDbParameter(key,OleDbType.VarChar);
}
else if (F2.ToUpper() ="=" "INTEGER")
{
par = new OleDbParameter(key,OleDbType.Integer);
}
else if (F2.ToUpper() ="=" "LSTRING")
{
par = new OleDbParameter(key,OleDbType.LongVarChar);
} // try
// {
par.Value = F1;
// }
// catch(Exception e)
// {
// throw new Exception(e.Message + " " + key + " " + F1 + " " + F2);
// }
return par;
}
public void ExecConfig(Hashtable ht,string sqls)
{
OleDbCommand cmd="new OleDbCommand();
connOpen();
ConfigCMD(ht,cmd,sqls);
ExecuteNonQuery(cmd);
}
public void ExecConfig(ArrayList alist,string sqls)
{
OleDbCommand cmd="new OleDbCommand();
connOpen();
ConfigCMD(alist,cmd,sqls);
ExecuteNonQuery(cmd);
}
/// <summary>
/// 数据操作
/// </summary>
/// <param name="cmd"></param>
private static void ExecuteNonQuery(OleDbCommand cmd)
{
Command.AccessClass cw = new AccessClass(); try
{
_returnValue = cmd.ExecuteNonQuery().ToString();
}
catch (Exception e) {throw new Exception("Run Exec Error!"+e.Message);}
finally
{
cmd.Parameters.Clear(); cw.connClose();
}
}
/// <summary>
/// sql语句型datatable
/// </summary>
/// <param name="sqls"></param>
/// <returns></returns>
public DataTable ReturnDatatableBySql(Hashtable ht,string sqls)
{
DataTable dt="new DataTable();
OleDbCommand cmd="new OleDbCommand();
ConfigCMD(ht,cmd,sqls);
OleDbDataAdapter sda="new OleDbDataAdapter(cmd);
sda.Fill(dt);
cmd.Parameters.Clear(); return dt;
}
public OleDbDataReader getReader(Hashtable ht,string sqls)
{
connOpen();
OleDbCommand cmd="new OleDbCommand();
ConfigCMD(ht,cmd,sqls);
OleDbDataReader reader = cmd.ExecuteReader();
return reader; //reader.Close();
//cmd.Parameters.Clear();
//connClose();
}
public OleDbDataReader getReader(ArrayList alist,string sqls)
{
connOpen();
OleDbCommand cmd="new OleDbCommand();
ConfigCMD(alist,cmd,sqls);
OleDbDataReader reader = cmd.ExecuteReader();
return reader; //reader.Close();
//cmd.Parameters.Clear();
//connClose();
} public string ReturnValue
{
get
{
return _returnValue;
}
set
{
_returnValue = value;
}
}
}
}

到此这篇关于asp.net(C#) Access 数据操作类就介绍到这了。生命太过短暂,今天放弃了明天不一定能得到。更多相关asp.net(C#) Access 数据操作类内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
ASP.NET轻量级MVC框架Nancy的基本用法

使用grpcui测试ASP.NET core的gRPC服务

ASP.NET Core中的对象池介绍

asp.net中MVC的处理流程详解

ASP.NET Core的日志系统介绍