几米天空

--------我的几米天空

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
   在现在项目中,数据库操作类是必不可少.网络上也有很多开源的,我想大家比较熟悉的应该是sqlhelper,自己现在用的这份类也是参照网上的资料和以前同事代码中改了些,代码如下:
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Web;
using EMEFRAME.CallDataBase;

namespace EMEFRAME.CallDataBase
{
    
/// <summary>
    
/// EMESqlHelper 的摘要说明。
    
/// </summary>

    public class EMESqlHelper: System.ComponentModel.Component
    
{
        SqlDataBase sql 
= new SqlDataBase();
        
/// <summary>
        
/// Sql 连接字符串,一般情况下在APPLICATION START 中 初始化 联机串
        
/// </summary>

        public static string CnConnectString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];


        
构造函数 初始化连接字符串

        
打开返回的事务

        
参数定义和转换
        
无XML版本代码

        
protected override void Dispose(bool disposing)
        
{
            sql.Disponse();
            
base.Dispose(disposing);
        }

    }

}


调用例子:
1> 得到记录数
        /// <summary>
        /// 得到记录数
        /// </summary>
        /// <returns>返回int</returns>
        public static int getWB_SYS_UserCount(string sWhere)
        {
            int iCount = 0;
            try
            {
                using EMESqlHelper es = new EMESqlHelper())
                {
                    iCount = es.GetRecordCount("WB_SYS_User", sWhere);
                }
                return iCount;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
2> 带返回参数的存储过程调用(注意其中的InputOutput类型)
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="_obj">WB_SYS_UserEnt实体</param>
        /// <returns>返回bool值</returns>
        public static bool WB_SYS_UserAdd(WB_SYS_UserEnt _obj)
        {
            //*******************定义并赋值参数数组*******************//
            SqlParameter[] arParms = new SqlParameter[9];
            arParms[0] = new SqlParameter("AdminGroupID", SqlDbType.Int, 4);
            arParms[0].Direction = ParameterDirection.Input;
            arParms[0].Value = _obj.AdminGroupID;
            arParms[1] = new SqlParameter("Username", SqlDbType.NChar, 40);
            arParms[1].Direction = ParameterDirection.Input;
            arParms[1].Value = _obj.Username;
            arParms[2] = new SqlParameter("Password", SqlDbType.NChar, 64);
            arParms[2].Direction = ParameterDirection.Input;
            arParms[2].Value = _obj.Password;
            arParms[3] = new SqlParameter("RealName", SqlDbType.VarChar, 20);
            arParms[3].Direction = ParameterDirection.Input;
            arParms[3].Value = _obj.RealName;
            arParms[4] = new SqlParameter("MobileNum", SqlDbType.VarChar, 20);
            arParms[4].Direction = ParameterDirection.Input;
            arParms[4].Value = _obj.MobileNum;
            arParms[5] = new SqlParameter("PasswordAsk", SqlDbType.VarChar, 200);
            arParms[5].Direction = ParameterDirection.Input;
            arParms[5].Value = _obj.PasswordAsk;
            arParms[6] = new SqlParameter("PasswordAnswer", SqlDbType.VarChar, 200);
            arParms[6].Direction = ParameterDirection.Input;
            arParms[6].Value = _obj.PasswordAnswer;
            arParms[7] = new SqlParameter("Creater", SqlDbType.VarChar, 20);
            arParms[7].Direction = ParameterDirection.Input;
            arParms[7].Value = _obj.Creater;
            bool Exist = false;
            arParms[8] = new SqlParameter("Exist", SqlDbType.Bit, 1);
            arParms[8].Direction = ParameterDirection.InputOutput;
            arParms[8].Value = Exist;
            try
            {
                using EMESqlHelper es = new EMESqlHelper())
                {
                    es.ExecCommand("WB_SYS_User_Add", "1", arParms);
                    Exist = (bool)arParms[8].Value;
                }
                return !Exist;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
其中关联类SqlDataBase和DataBase的代码如下:
using System;
using System.Data;
using System.Data.SqlClient;

namespace EMEFRAME.CallDataBase
{
    
/// <summary>
    
/// SqlDataBase 的摘要说明。
    
/// </summary>

    public class SqlDataBase : DataBase
    
{
        
数据属性 
        
public SqlDataBase()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }


        
打开数据库

        
执行存储过程

        
执行 CMD 

        
返回DATASET 

        
返回READER 

        
卸载内存 


    }

}


using System;
using System.Data;

namespace EMEFRAME.CallDataBase
{
    
/// <summary>
    
/// DataBase 的摘要说明。
    
/// </summary>

    public abstract class DataBase
    
{
        
public DataBase()
        
{
        }


        
public abstract bool OpenCn ();
        
public abstract IDbConnection OpenAndReturnCn();
        
public abstract IDataReader GetReader ();
        
public abstract DataSet GetDataSet (string TableName);
        
public abstract DataSet GetDataSet();
        
public abstract void CmdExecNonQuery ();
        
public abstract object CmdExecScalar ();
        
public abstract void CloseCn ();
        
public abstract void CloseReader ();
        
public abstract void ClearParameter ();
        
public abstract void Disponse ();
    }

}

posted on 2007-07-09 11:09  几米天空  阅读(840)  评论(2编辑  收藏  举报