当前位置:编程学习 > C#/ASP.NET >>

给你个例子.NET中三层构架开发实例 - 用户注册系统

答案:数据库基类
using System;
using System.Data;
using System.Data.SqlClient;

namespace HaiSky.HtJob
{
    /// <summary>
    /// DbClass 的摘要说明。
    /// </summary>
    public class DbClass
    {
        private string connectionString;
        protected SqlConnection Connection;
        public DbClass(string newConnectionString)
        {
            connectionString = newConnectionString;
            Connection = new SqlConnection(connectionString);
        }
        public string ConnectionString
        {
            get
            {
                return connectionString;
            }
        }
        private SqlCommand BuildQueryCommand(string storedProcName,IDataParameter[] parameters)
        {
            SqlCommand command = new SqlCommand(storedProcName,Connection);
            command.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter parameter in parameters)
                {
                    command.Parameters.Add(parameter);
                }
            return command;
        }
        private SqlCommand BuildIntCommand(string storedProcName,IDataParameter[] parameters)
        {
            SqlCommand command = BuildQueryCommand(storedProcName,parameters);
            command.Parameters.Add(new SqlParameter("ReturnValue",SqlDbType.Int,4,
                ParameterDirection.ReturnValue,false,
                0,
                0,
                string.Empty,DataRowVersion.Default,null));
            return command;        
        }
        protected int RunProcedure(string storedProcName,IDataParameter[] parameters,out int rowsAffected)
        {
            int result;
            Connection.Open();
            SqlCommand command = BuildIntCommand(storedProcName,parameters);
            rowsAffected = command.ExecuteNonQuery();
            result = (int)command.Parameters["ReturnValue"].Value;
            Connection.Close();
            return result;
        }
        protected SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters )
        {
            SqlDataReader returnReader;

            Connection.Open();
            SqlCommand command = BuildQueryCommand( storedProcName, parameters );
            command.CommandType = CommandType.StoredProcedure;

            returnReader = command.ExecuteReader();
            return returnReader;
        }
        protected DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName )
        {
            DataSet dataSet = new DataSet();
            Connection.Open();
            SqlDataAdapter sqlDA = new SqlDataAdapter();
            sqlDA.SelectCommand = BuildQueryCommand( storedProcName, parameters );
            sqlDA.Fill( dataSet, tableName );
            Connection.Close();

            return dataSet;
        }
        protected void RunProcedure(string storedProcName, IDataParameter[] parameters, DataSet dataSet, string tableName )
        {
            Connection.Open();
            SqlDataAdapter sqlDA = new SqlDataAdapter();
            sqlDA.SelectCommand = BuildIntCommand( storedProcName, parameters );
            sqlDA.Fill( dataSet, tableName );
            Connection.Close();            
        }
    }
}

商务层基类(逻辑层)
using System;
namespace Wrox.WebModules.Business
{
    public class BizObject
    {
        public BizObject()
        {        

上一个:佳文推荐:运用VB.NET的面向对象的特征
下一个:一番话:有关组成模式

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,