zoukankan      html  css  js  c++  java
  • C#编写的数据操作组建,可用于数据库操作层

    在web系统开发中经常要对数据库进行频繁操作,本人写了一个组建封装了数据库业务处理的大部分操作,采用连接池技术并优化了数据库连接,

    提高了数据库操作性能,减少了数据操作代码的编写,数据连接串进行了简单加密防止在web.config中看到明文,希望能给大家带来方便。

    类库文件名:DataAccess.DLL

    类库空间名:Cweb.DBCls

    数据业务处理类名:BLL

     作者:邬畏畏

    一.数据操作类的接口

    public static bool autoCloseConForReader;

    描述:

    为true表示关闭reader时候关闭连接。

     

    public static List<SqlConnection> ListCon;

    描述:

    连接池,存放连接。

     

    public static bool Enc { get; set; }

    描述:

    设施数据库连接串是否是加密串,true表示数据库连接串是加密的。

     

    public static int EncN { get; set; }

    描述:

    加密数值,默认是5,可以自定义。

     

    public static void CloseAllCon();

    描述:

    关闭所有链接。

     

    public static bool Insert(string tableName, string fields, string values);

    描述:

    插入记录。

    示例:

    BLL.Insert("RTU","Rtu_name,reg_time" , " 'myrtu ', '2011-9-9'");

     

    public static bool InsertFromSelect(string tableName, string fields, string selectSQL);

    描述:

    插入记录。

    示例:

    BLL.InsertFromSelect("web_popmenu", "menuname","select menuname from tableName");

     

    public static bool InsertFromSelect(string tableName, string fields, string selectField, string selectTableName, string where);

    描述:

    插入记录。

    示例:

    BLL.InsertFromSelect("web_popmenu", "menuname","menuname","web_menu","id=5");

     

     

    public static byte ReturnByte(string myField, string myTable, string where);

    描述:

    返回单个值,类型为字节。

     

    public static double ReturnDBL(string myField, string myTable, string where);

    描述:

    返回单个值,类型为double。

     

    public static DataTable ReturnDT(string myField, string myTable, string where, int RecCnt);

    描述:

    返回DataTable对象。

    where 为空表示无需条件。

    RecCnt 为0表示返回满足条件的记录,为n表示返回指定个数的记录。

     

    public static int ReturnInt(string myField, string myTable, string where);

    描述:

    返回单个值,类型为int。

     

    public static short ReturnInt16(string myField, string myTable, string where);

    描述:

    返回单个值,类型为short。

     

    public static SqlDataReader ReturnReader(string myField, string myTable, string where, int RecCnt);

    描述:

    返回SqlDataReader对象。

     

    public static string ReturnStr(string myField, string myTable, string where);

    描述:

    返回单个值,类型为string。

     

    public static void RunSQL(string SQL);

    描述:

    执行SQL语句,可以是insert ,update ,delete 。

    public static bool Delete(string tableName, string PKey , string value)

    描述:

    删除记录。

    二.Web.config配置

    在web.config中加入下面配置节

    <appSettings>

        <add key="conn" value="Ifyf%XtzwhjB3@Nsnynfq%HfyfqtlBmx@Ujwxnxy%Xjhzwny~%NsktBYwzj@Zxjw%NIBxf@Ufxx|twiB|||"/>

    <add key="ENC" value="true"/>

    <add key="PoolCount" value="500"/>

      </appSettings>

    说明:

    key="conn"

    value用于设置数据库连接串。可以是明文也可以是密文。

    key="ENC"

    value 为true 或 false ,用来设置是否使用加密数据库连接串。

    key=" PoolCount"

    value是连接池大小。

    代码示例:

    using Cweb.DBCls;

    示例一:

    DataTable dt = BLL.ReturnDT("*", "RTU", "", 0);
    GridView1.DataSource = dt;
    GridView1.DataBind();

    示例二:

    DataTable dt = BLL.ReturnDT("extent_name,extent_addr,rtu_name", "RTU A INNER JOIN extent B ON A.extent_code = B.extent_code", "", 0);
    GridView1.DataSource = dt;
    GridView1.DataBind();

    示例三:

     int c = BLL.ReturnInt("count(*)", "RTU", "");
    Response.Write("RecordCount:" + c);

    示例四:

    BLL.Delete("web_popmenu", "id", "11");

    下载地址:https://files.cnblogs.com/wsoft/DataAccess.rar

  • 相关阅读:
    开启CTF大门
    关于windows下scapy出现log_runtime问题
    Python关于Threading暂停恢复解决办法
    angr入门之CLE
    Linux信号量
    IDApython 命令
    Array 数组对象
    随机数 random()
    四舍五入round()
    向下取整floor()
  • 原文地址:https://www.cnblogs.com/wsoft/p/1956662.html
Copyright © 2011-2022 走看看