把数据库操作写成一个类,谁有写好的代码

解决方案 »

  1.   

    Data Access Application Blockhttp://www.microsoft.com/china/msdn/archives/library/dnbda/html/daab-rm.asp
      

  2.   

    那些都不怎么好啊,还是ORM,数据库对象映射好,Nhibernate,研究研究吧,我正看呢,啃得有点艰辛……5555
      

  3.   

    Data Access Application Block 哪有中文版??? 包括说明书CHM也是中文的
      

  4.   

    http://blog.csdn.net/huangyaoshifog/archive/2004/08/27/86263.aspx
      

  5.   

    使用Petshop里面的吧,跟上面差不多,但相对简单些
      

  6.   

    using System;
    using System.Data.SqlClient;
    using System.Data;
    namespace DataAccess
    {
    /// <summary>
    /// It is used to asscess database
    /// </summary>
    public class Database:IDisposable
    {    
    /// <summary>
    /// It's store the error message
    /// </summary>
    string _errorMessage;
    /// <summary>
    ///It's store the Exception 
    /// </summary>
    System.Exception _exception;
    /// <summary>
    /// It is ed run state.
    /// </summary>
    int result;
    /// <summary>
    /// store Connection String
    /// </summary>
    //string _connectionString; SqlConnection myConnection; SqlCommand myCommand; string _ConfigurationConnectionString=null;
         #region IDisposable 成员 public void Dispose()
    {
    if(this.myConnection==null)
    myConnection.Dispose();
    else
    myConnection=null;
    System.GC.SuppressFinalize(this);             } #endregion
    /// <summary>
    /// get or set  Exception as string .
    /// </summary>
    /// 
    public string ErrorMessage
    {
    get{ return _errorMessage;}
    set{ _errorMessage=value;}
    }
    public int Result
    {
    get{return result;}
    } /// <summary>
    /// get or set Exception.
    /// </summary>
    public Exception Exception
    {
    get{return _exception;}
    set{_exception=value;} }
    /// <summary>
    /// Set ConnectionString where in Web.Config
    /// </summary>
    public string ConfigurationConnectionString
    {
    get{return this._ConfigurationConnectionString;}
    set{this._ConfigurationConnectionString=value;}
    } /// <summary>
    /// get Connetion string from web.Config.if you hava not set the ConfigurationConnection.
    /// it will get AppSettings by connectionString,if you want set the ConnectionString you 
    /// can use it.
    /// </summary>
     private string ConnectionString
    {
    get{
    if(this._ConfigurationConnectionString==null)
    {
    return (string)System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
    }
    else
    {
                       return (string)this._ConfigurationConnectionString; }

    } }


    /// <summary>
    /// Create OutPut SqlParameter.
    /// </summary>
    /// <param name="_name">Parameter's Name</param>
    /// <param name="_value">Parameter's Value</param>
    /// <returns></returns>
    public SqlParameter MakeParameter(string _name,object _value)
    {

    SqlParameter myParameter=new SqlParameter(_name,_value);
    myParameter.Direction=ParameterDirection.Input;
    return myParameter;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="_name"></param>
    /// <param name="_value"></param>
    /// <param name="_direction"></param>
    /// <returns></returns>
    public SqlParameter MakeParameter(string _name,object _value,ParameterDirection  _direction)
    {
    SqlParameter myParameter=new SqlParameter(_name,_value);
            myParameter.Direction=_direction;
     return myParameter;

    }
    /// <summary>
    /// The Procedure which have no parameter and 
    /// return scalar.
    /// </summary>
    /// <param name="_procedureName"></param>
    /// <returns></returns>
    public void RunProcedure(string _procedureName)
    {
    try
    {
    myConnection=new SqlConnection(this.ConnectionString);
    myCommand=new SqlCommand(_procedureName,myConnection);
    myCommand.CommandType=CommandType.StoredProcedure;
    myConnection.Open();
    //this.result=myCommand.ExecuteNonQuery();
    myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

    }
    catch(Exception myEx)
    {
    this.ErrorMessage=myEx.Message;
    this.Exception=myEx;
    this.result =-1;
    }
    finally
    {
    myConnection.Close();
    } }        /// <summary>
            /// The Procedure which have none parameter and return SqlDataReader.
            /// </summary>
            /// <param name="_procedureName"></param>
            /// <returns></returns>
    public void RunProcedure(string _procedureName, ref SqlDataReader _datareader)
    {
    try
    {
    myConnection=new SqlConnection(this.ConnectionString);
    myCommand=new SqlCommand(_procedureName,myConnection);
    myCommand.CommandType=CommandType.StoredProcedure;
    myConnection.Open();
    _datareader=myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    }
    catch(Exception myEx)
    {
    this.ErrorMessage=myEx.Message;
    this.Exception=myEx;

    this.result=-1;

    }
    }
    /// <summary>
    /// The Procedure which have none parameter and return DataSet.
    /// </summary>
    /// <param name="_procedureName"></param>

    public void RunProcedure(string _procedureName,ref DataSet _dataset)
    {    
    try{
    myConnection=new SqlConnection(this.ConnectionString);
    myCommand=new SqlCommand(_procedureName,myConnection);
    myCommand.CommandType=CommandType.StoredProcedure;
    myConnection.Open();
    SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
    myAdapter.Fill(_dataset);
    }
    catch(Exception myEx)
    {
    this.ErrorMessage=myEx.Message;
    this.Exception=myEx;

    result=-1;

    }
    }
    /// <summary>
    /// The Procedure which has Parameter's Array and return SqlDataReader.
    /// </summary>
    /// <param name="_procedureName"></param>
    /// <param name="_parameters"></param>
    /// <returns></returns>
    public void RunProcedure(string _procedureName,SqlParameter[] _parameters,ref SqlDataReader _sqldataReader)
    {
    try
    {
    myConnection=new SqlConnection(this.ConnectionString);
    myCommand=new SqlCommand(_procedureName,myConnection);
    myCommand.CommandType=CommandType.StoredProcedure;
    foreach(SqlParameter param in _parameters)
    myCommand.Parameters.Add(param); myConnection.Open();
    _sqldataReader=myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    }
    catch(Exception myEx)
    {
    this.ErrorMessage=myEx.Message;
    this.Exception=myEx;
    result=-1;

    }
    }
    /// <summary>
    /// The Procedure which has Parameter's Array and return DataSet.
    /// </summary>
    /// <param name="_procedureName"></param>
    /// <param name="_parameters"></param>
    /// <returns></returns>
    public void RunProcedure(string _procedureName,SqlParameter[] _parameters,ref DataSet _dataset)
    {
    try
    {
    myConnection=new SqlConnection(this.ConnectionString);
    myCommand=new SqlCommand(_procedureName,myConnection);
    myCommand.CommandType=CommandType.StoredProcedure;
    foreach(SqlParameter param in _parameters)
    myCommand.Parameters.Add(param); myConnection.Open();
    SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
    myAdapter.Fill(_dataset);
    }
    catch(Exception myEx)
    {
    this.ErrorMessage=myEx.Message;
    this.Exception=myEx;
    this.result=-1;

    } }
    /// <summary>
    /// The Procedure which has Parameter's Array and return Scale.
    /// </summary>
    /// <param name="_procedureName"></param>
    /// <param name="_parameters"></param>
    /// <returns></returns>
    public void RunProcedure(string _procedureName,SqlParameter[] _parameters)
    {
    try
    {
    myConnection=new SqlConnection(this.ConnectionString);
    myCommand=new SqlCommand(_procedureName,myConnection);
    myCommand.CommandType=CommandType.StoredProcedure;
    foreach(SqlParameter param in _parameters)
    myCommand.Parameters.Add(param); myConnection.Open();
    //this.result=myCommand.ExecuteNonQuery();
    myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

    }
    catch(Exception myEx)
    {
    this.ErrorMessage=myEx.Message;
    this.Exception=myEx;
    this.result=-1;

    } }

    }
    }看看