欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-09 12:57:09
复制代码 代码如下:using system; using system.configuration; using system.data; using system.da...
复制代码 代码如下:

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