asp.net(C#) Access数据操作类如何实现代码

来源:爱站网时间:2022-06-01编辑:网友分享
不知道朋友们想不想知道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 textarea替换html特殊字符的代码

下一篇:asp.net 2.0安装fileupload控件后可随意上传文件

相关阅读

热门软件源码

最新软件源码下载