亲宝软件园·资讯

展开

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

人气:0
想了解asp.net(C#) Access 数据操作类的相关内容吗,在本文为您仔细讲解Access 数据操作类的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:asp.net,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;
}
}
}
}

加载全部内容

相关教程
猜你喜欢
用户评论