数据库操作类实现(C#,SqlClient)

使用ADO.NET时,每次数据库操作都要设置connection属性、建立connection、使用command、事务处理等,比较繁琐,有很多重复工作。能不能把这些繁琐的、常用的操作再封装一下,以更方便、安全地使用。
None.gif using  System; 
None.gif
using  System.Data.SqlClient; 
None.gif
using  System.Text; 
None.gif
using  System.Data; 
None.gif
using  System.Collections; 
None.gif
using  System.Configuration; 
None.gif
None.gif
None.gif
public   class  DBAccess 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Declare the ole db required objects 
InBlock.gif
/// </summary> 
InBlock.gif
InBlock.gif
InBlock.gif
/// <summary> 
InBlock.gif
/// An ole db adapter to act as the bridge to the database 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate SqlDataAdapter dbDataAdapter; 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// The connection to the database 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate SqlConnection dbConnection; 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// The command for doing the inserts 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate SqlCommand dbInsertCommand; 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// The command for doing the deletes 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate SqlCommand dbDeleteCommand; 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// The command for doing the updates 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate SqlCommand dbUpdateCommand; 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// The command for doing the Selects 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate SqlCommand dbSelectCommand; 
InBlock.gif
InBlock.gif
private SqlCommand dbSelectCommandofAdapter; 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// The command for get dataset 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate SqlDataAdapter dataAdapterCommand; 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// The data reader for the application 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifpublic SqlDataReader dbDataReader; 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Declare an enum to allow internal tracking of commands 
ExpandedSubBlockEnd.gif
/// </summary> 

ExpandedSubBlockStart.gifContractedSubBlock.gifenum COMMANDdot.gif{ NONE, INSERT, UPDATE, DELETE, SELECT,DATASET }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Internal member for tracking command progress 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate COMMAND command; 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// String to hold error messages if a command fails 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate string error; 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Get a stored error message if ExecuteCommand fails 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifpublic string ErrorMessage 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return error; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// bool holder for is open 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate bool bOpen; 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Check to see if a data base is open 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifpublic bool IsOpen 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return bOpen; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Declare a string object for the insert command 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifpublic string InsertCommand 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return dbInsertCommand.CommandText; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifcommand 
= COMMAND.INSERT; 
InBlock.gifdbInsertCommand.CommandText 
= value; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Declare a string object for the delete command 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifpublic string DeleteCommand 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return dbDeleteCommand.CommandText; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifcommand 
= COMMAND.DELETE; 
InBlock.gifdbDeleteCommand.CommandText 
= value; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Declare a string object for the update command 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifpublic string UpdateCommand 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return dbUpdateCommand.CommandText; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifcommand 
= COMMAND.UPDATE; 
InBlock.gifdbUpdateCommand.CommandText 
= value; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Declare a string object for the select command 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifpublic string SelectCommand 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return dbSelectCommand.CommandText; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifcommand 
= COMMAND.SELECT; 
InBlock.gifdbSelectCommand.CommandText 
= value; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
public string SelectDataSetCommand 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return dataAdapterCommand.SelectCommand.CommandText; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifcommand 
= COMMAND.DATASET; 
InBlock.gifdataAdapterCommand.SelectCommand.CommandText 
= value; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Get the reader from the class 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifpublic SqlDataReader GetReader 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
switch( command ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
case COMMAND.NONE: return null
InBlock.gif
case COMMAND.DELETE: return DeleteReader; 
InBlock.gif
case COMMAND.INSERT: return InsertReader; 
InBlock.gif
case COMMAND.SELECT: return SelectReader; 
InBlock.gif
case COMMAND.UPDATE: return UpdateReader; 
InBlock.gif
defaultreturn null
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
public DataSet GetDataSet 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
switch( command ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
case COMMAND.DATASET: return SelectDataSet(); 
InBlock.gif
defaultreturn null
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
public DataSet SelectDataSet() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifdataAdapterCommand.SelectCommand.Connection 
= dbConnection; 
InBlock.gifDataSet dataset 
= new DataSet(); 
InBlock.gifdataAdapterCommand.Fill(dataset); 
InBlock.gif
return dataset; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch (Exception exp) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.giferror 
= exp.Message; 
InBlock.gif
return null
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Execute the command that has been set up previously 
InBlock.gif
/// </summary> 
ExpandedSubBlockEnd.gif
/// <returns>A boolean value indicating true or false</returns> 

InBlock.gifpublic bool ExecuteCommand() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
bool bReturn = false
InBlock.gif
if( command == COMMAND.NONE ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return bReturn; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else if( command == COMMAND.SELECT ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// select only returns true as the get reader function will 
ExpandedSubBlockEnd.gif
/// execute the command 

InBlock.gif
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if( dbDataReader != null ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifdbDataReader.Close(); 
InBlock.gifdbDataReader 
= null
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gifbReturn 
= true
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// return bReturn; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch( SqlException exp ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.giferror 
= "dbException thrown when trying to Select, error given = " + exp.Message + " check the sql"
InBlock.gif
return bReturn = false
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else if( command == COMMAND.DATASET ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return bReturn; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
int nAffected = -1
InBlock.gif
InBlock.gif
if( dbDataReader != null ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifdbDataReader.Close(); 
InBlock.gifdbDataReader 
= null
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// get the transaction object from the connection 
InBlock.gifSqlTransaction trans = dbConnection.BeginTransaction(); 
InBlock.gif
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// create a nested transaction on the connection transaction 
InBlock.gifswitch( command ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
case COMMAND.DELETE: dbDeleteCommand.Transaction = trans; break
InBlock.gif
case COMMAND.INSERT: dbInsertCommand.Transaction = trans; break
InBlock.gif
case COMMAND.UPDATE: dbUpdateCommand.Transaction = trans; break
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// execute the command 
InBlock.gifswitch( command ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
case COMMAND.DELETE: nAffected = dbDeleteCommand.ExecuteNonQuery(); break
InBlock.gif
case COMMAND.INSERT: nAffected = dbInsertCommand.ExecuteNonQuery(); break
InBlock.gif
case COMMAND.UPDATE: nAffected = dbUpdateCommand.ExecuteNonQuery(); break
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch( InvalidOperationException ioexp ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifStringBuilder buildError 
= new StringBuilder(); 
InBlock.gifbuildError.Append( 
"InvalidOperationException thrown when trying to " ); 
InBlock.gif
InBlock.gif
switch( command ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
case COMMAND.DELETE: buildError.Append( "Delete" ); break
InBlock.gif
case COMMAND.INSERT: buildError.Append( "Insert" ); break
InBlock.gif
case COMMAND.UPDATE: buildError.Append( "Update" ); break
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gifbuildError.Append( 
", error given = " + ioexp.Message + " check the sql" ); 
InBlock.gif
InBlock.giferror 
= buildError.ToString(); 
InBlock.gif
InBlock.gif
return bReturn = false
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch( SqlException dbexp ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifStringBuilder buildError 
= new StringBuilder(); 
InBlock.gifbuildError.Append( 
"InvalidOperationException thrown when trying to " ); 
InBlock.gif
InBlock.gif
switch( command ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
case COMMAND.DELETE: buildError.Append( "Delete" ); break
InBlock.gif
case COMMAND.INSERT: buildError.Append( "Insert" ); break
InBlock.gif
case COMMAND.UPDATE: buildError.Append( "Update" ); break
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gifbuildError.Append( 
", error given = " + dbexp.Message + " check the sql" ); 
InBlock.gif
InBlock.giferror 
= buildError.ToString(); 
InBlock.gif
InBlock.gif
return bReturn = false
ExpandedSubBlockEnd.gif}
 
InBlock.gif
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// commit the command 
InBlock.gifif( nAffected == 1 ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
switch( command ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
case COMMAND.DELETE: dbDeleteCommand.Transaction.Commit(); break
InBlock.gif
case COMMAND.INSERT: dbInsertCommand.Transaction.Commit(); break
InBlock.gif
case COMMAND.UPDATE: dbUpdateCommand.Transaction.Commit(); break
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
//trans.Commit(); 
InBlock.gif

InBlock.gifbReturn 
= true
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockStart.gifContractedSubBlock.gif
else /**//// if something went wrong rollback 
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif
InBlock.gif
switch( command ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
case COMMAND.DELETE: dbDeleteCommand.Transaction.Rollback(); break
InBlock.gif
case COMMAND.INSERT: dbInsertCommand.Transaction.Rollback(); break
InBlock.gif
case COMMAND.UPDATE: dbUpdateCommand.Transaction.Rollback(); break
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
//trans.Rollback(); 
InBlock.gif

InBlock.gifbReturn 
= false
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
return bReturn; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
select functions#region select functions 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Get the Select reader from the select command 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate SqlDataReader SelectReader 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if( dbDataReader != null ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if( dbDataReader.IsClosed == false ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifdbDataReader.Close(); 
InBlock.gifdbDataReader 
= null
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gifdbDataReader 
= dbSelectCommand.ExecuteReader(); 
InBlock.gif
return dbDataReader; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Get the Update reader from the update command 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate SqlDataReader UpdateReader 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if( dbDataReader.IsClosed == false ) 
InBlock.gifdbDataReader.Close(); 
InBlock.gif
InBlock.gifdbDataReader 
= dbSelectCommand.ExecuteReader(); 
InBlock.gif
return dbDataReader; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Get the Insert Reader from the Insert Command 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate SqlDataReader InsertReader 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if( dbDataReader.IsClosed == false ) 
InBlock.gifdbDataReader.Close(); 
InBlock.gif
InBlock.gifdbDataReader 
= dbSelectCommand.ExecuteReader(); 
InBlock.gif
return dbDataReader; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Get the Delete Reader from the Delete Command 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifprivate SqlDataReader DeleteReader 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if( dbDataReader != null ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if( dbDataReader.IsClosed == false ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifdbDataReader.Close(); 
InBlock.gifdbDataReader 
= null
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gifdbDataReader 
= dbSelectCommand.ExecuteReader(); 
InBlock.gif
return dbDataReader; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif
#endregion
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Standard Constructor 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifpublic DBAccess() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// NOTE That we are not setting the commands up the way the wizard would 
ExpandedSubBlockEnd.gif
/// but building them more generically 

InBlock.gif
InBlock.gif
// create the command variables 
InBlock.gif
dbDataAdapter = new SqlDataAdapter(); 
InBlock.gifdbConnection 
= new SqlConnection(); 
InBlock.gifdbSelectCommand 
= new SqlCommand(); 
InBlock.gifdbDeleteCommand 
= new SqlCommand(); 
InBlock.gifdbUpdateCommand 
= new SqlCommand(); 
InBlock.gifdbInsertCommand 
= new SqlCommand(); 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// set up the adapter 
InBlock.gifdbDataAdapter.DeleteCommand = dbDeleteCommand; 
InBlock.gifdbDataAdapter.InsertCommand 
= dbInsertCommand; 
InBlock.gifdbDataAdapter.SelectCommand 
= dbSelectCommand; 
InBlock.gifdbDataAdapter.UpdateCommand 
= dbUpdateCommand; 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// make sure everyone knows what conection to use 
InBlock.gifdbSelectCommand.Connection = dbConnection; 
InBlock.gifdbDeleteCommand.Connection 
= dbConnection; 
InBlock.gifdbUpdateCommand.Connection 
= dbConnection; 
InBlock.gifdbInsertCommand.Connection 
= dbConnection; 
InBlock.gif
InBlock.gifcommand 
= COMMAND.NONE; 
InBlock.gifdbDataReader 
= null
InBlock.gif
InBlock.gifdbSelectCommandofAdapter 
= new SqlCommand(); 
InBlock.gifdataAdapterCommand 
= new SqlDataAdapter(); 
InBlock.gifdataAdapterCommand.SelectCommand 
= dbSelectCommandofAdapter; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
public void Open() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// set up the connection string 
InBlock.gifStringBuilder strBuild = new StringBuilder(); 
InBlock.gif
InBlock.gif
//Connection的属性从配置文件读取 
InBlock.gif
strBuild.AppendFormat(ConfigurationSettings.AppSettings["DBConnection"]); 
InBlock.gif
InBlock.gifdbConnection.ConnectionString 
= strBuild.ToString(); 
InBlock.gif
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifdbConnection.Open(); 
InBlock.gifbOpen 
= true
ExpandedSubBlockEnd.gif}
 
InBlock.gif
catch (Exception exp) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.giferror 
= exp.Message; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// Close the currently open connection 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifpublic void Close() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if (dbDataReader != null
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if( dbDataReader.IsClosed == false ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifdbDataReader.Close(); 
InBlock.gifdbDataReader 
= null
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gifdbConnection.Close(); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedBlockEnd.gif}
 
None.gif
None.gif
None.gif
None.gif使用示例: 
None.gif
None.gifInsert操作,新建用户: 
None.gif
None.gif
public   bool  NewUser() 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gifDBAccess newUserDBAccess 
= new DBAccess(); 
InBlock.gifStringBuilder sqlStr 
= new StringBuilder(); 
InBlock.gifsqlStr.Append( 
"Insert into userTable(usrName,pwd,name,depart,role,available) values("); 
InBlock.gifsqlStr.Append( 
"''" + usrName + "'',"); 
InBlock.gifsqlStr.Append( 
"''" + pwd + "'',"); 
InBlock.gifsqlStr.Append( 
"''" + name + "'',"); 
InBlock.gifsqlStr.Append( 
"''" + depart + "'',"); 
InBlock.gifsqlStr.Append( 
"''" + role + "'',"); 
InBlock.gifsqlStr.Append(
1); 
InBlock.gifsqlStr.Append( 
")"); 
InBlock.gif
InBlock.gifnewUserDBAccess.InsertCommand 
= sqlStr.ToString(); 
InBlock.gifnewUserDBAccess.Open(); 
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if (!newUserDBAccess.ExecuteCommand()) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.giferrMsg 
= newUserDBAccess.ErrorMessage; 
InBlock.gif
InBlock.gif
return false
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return true
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifnewUserDBAccess.Close(); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedBlockEnd.gif}
 
None.gif
None.gif
None.gif
None.gifUpdate操作,修改用户信息: 
None.gif
None.gif
public   bool  ModifyUser() 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gifDBAccess modifyUserDBAccess 
= new DBAccess(); 
InBlock.gifStringBuilder sqlStr 
= new StringBuilder(); 
InBlock.gifsqlStr.Append( 
"update userTable set "); 
InBlock.gifsqlStr.Append( 
" usrName = "); 
InBlock.gifsqlStr.Append( 
"''" + usrName + "'',"); 
InBlock.gifsqlStr.Append( 
" name ="); 
InBlock.gifsqlStr.Append( 
"''" + name + "'',"); 
InBlock.gifsqlStr.Append( 
" pwd ="); 
InBlock.gifsqlStr.Append( 
"''" + pwd + "'',"); 
InBlock.gifsqlStr.Append( 
" depart ="); 
InBlock.gifsqlStr.Append( 
"''" + depart + "'',"); 
InBlock.gifsqlStr.Append( 
" role ="); 
InBlock.gifsqlStr.Append( 
"''" + role + "''"); 
InBlock.gifsqlStr.Append( 
" where usrID = "); 
InBlock.gifsqlStr.Append(id); 
InBlock.gif
InBlock.gifmodifyUserDBAccess.UpdateCommand 
= sqlStr.ToString(); 
InBlock.gifmodifyUserDBAccess.Open(); 
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if (!modifyUserDBAccess.ExecuteCommand()) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.giferrMsg 
= modifyUserDBAccess.ErrorMessage; 
InBlock.gif
InBlock.gif
return false
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return true
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifmodifyUserDBAccess.Close(); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedBlockEnd.gif}
 
None.gif
None.gif
None.gif
None.gifDelete操作,删除用户: 
None.gif
public   static   bool  DelUser( int  usrID) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gifDBAccess delUserDBAccess 
= new DBAccess(); 
InBlock.gifStringBuilder sqlStr 
= new StringBuilder(); 
InBlock.gifsqlStr.Append( 
"update userTable set "); 
InBlock.gifsqlStr.Append( 
" available ="); 
InBlock.gifsqlStr.Append(
0); 
InBlock.gifsqlStr.Append( 
" where usrID = "); 
InBlock.gifsqlStr.Append(usrID); 
InBlock.gif
InBlock.gif
InBlock.gifdelUserDBAccess.UpdateCommand 
= sqlStr.ToString(); 
InBlock.gifdelUserDBAccess.Open(); 
InBlock.gif
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if (!delUserDBAccess.ExecuteCommand()) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return false
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return true
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifdelUserDBAccess.Close(); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedBlockEnd.gif}
 
None.gif

转载于:https://www.cnblogs.com/nbwzy/archive/2006/06/04/wzy.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值