using System;
using System.Data.SqlClient;
using System.Data;
namespace ASPNetPortal
{
/// <summary>
/// It is used to access database
/// </summary>
/// -----------------------------------------------------
/// -----------------------------b0b0 2004-9.3----------
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 marked run state.
/// </summary>
int result;
/// <summary>
/// store Connection String
/// </summary>
//string _connectionString;
SqlConnection myConnection=null;
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
///( rewrite a bug that can't Set ConfigurationConnectionString 2004-10-8 b0b0)
///
/// </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.
/// rewrite a bug(property ConnectionString when this._ConfigurationConnectionString!=null
/// return a error ConnectionString) 2004-10-8
/// </summary>
private string ConnectionString
{
get{
if(this._ConfigurationConnectionString==null)
{
return (string)System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
}
else
{
return (string)this._ConfigurationConnectionString;
}
}
}
//----------------------------------------------------------------
//Modify by b0b0 2005-04-18
//Description:Modify turn off Connection
public void CloseConnection()
{
if(myConnection.State==System.Data.ConnectionState.Open)
{
myConnection.Close();
}
}
public Database()
{
myConnection=new SqlConnection(this.ConnectionString);
}
//----------------------------------------------------------------
/// <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;
}
public SqlParameter MakeParameter(string _name,object _value,ParameterDirection _direction)
{
SqlParameter myParameter=new SqlParameter(_name,_value);
myParameter.Direction=_direction;
return myParameter;
}
/// <summary>
///
/// </summary>
/// <param name="_name"></param>
/// <param name="_value"></param>
/// <param name="_direction"></param>
/// <returns></returns>
public SqlParameter MakeParameter(string _name,System.Data.SqlDbType _DbType,ParameterDirection _direction)
{
SqlParameter myParameter=new SqlParameter(_name,_DbType);
myParameter.Direction=_direction;
return myParameter;
}
public SqlParameter MakeParameter(string _name,System.Data.SqlDbType _DbType,ParameterDirection _direction,int _size)
{
SqlParameter myParameter=new SqlParameter(_name,_DbType);
myParameter.Direction=_direction;
myParameter.Size=_size;
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();
//another bug when myCommand.Executor CommandText which contain UPDATE、INSERT 和 DELETE
//myCommand.ExecuteNonQuery()==-1 and u set this.result=-1,at last u make user felt there must have Exception ,but there has not .
//----Rewrite at 2004-10-20 b0b0----------------------------------------------------------
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;
}
finally
{
myConnection.Close();
}
}
/// <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;
result=-1;
}
finally
{
myConnection.Close();
}
}
/// <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();
myCommand.ExecuteNonQuery();
}
catch(Exception myEx)
{
this.ErrorMessage=myEx.Message;
this.Exception=myEx;
result=-1;
}
finally
{
myConnection.Close();
}
}
}
}
到底问题出在那块儿?