数据库操作类实现(C#,SqlClient)
文章作者 100test 发表时间 2008:01:04 11:54:15
来源 100Test.Com百考试题网
使用ADO.Net时,每次数据库操作都要设置connection属性、建立connection、使用command、事务处理等,比较繁琐,有很多重复工作。能不能把这些繁琐的、常用的操作再封装一下,以更方便、安全地使用。下面这个类就是一种尝试:
using System.
using System.Data.SqlClient.
using System.Text.
using System.Data.
using System.Collections.
using System.Configuration.
public class DBAccess
{
///
/// Declare the ole db required objects
///
///
/// An ole db adapter to act as the bridge to the database
///
private SqlDataAdapter dbDataAdapter.
///
/// The connection to the database
///
private SqlConnection dbConnection.
///
/// The command for doing the inserts
///
private SqlCommand dbInsertCommand.
///
/// The command for doing the 0deletes
///
private SqlCommand dbDeleteCommand.
///
/// The command for doing the 0updates
///
private SqlCommand dbUpdateCommand.
///
/// The command for doing the Selects
///
private SqlCommand dbSelectCommand.
private SqlCommand dbSelectCommandofAdapter.
///
/// The command for get dataset
///
private SqlDataAdapter dataAdapterCommand.
///
/// The data reader for the application
///
public SqlDataReader dbDataReader.
///
/// Declare an enum to allow internal tracking of commands
///
enum COMMAND{ NONE, INSERT, UPDATE, DELETE, SELECT,DATASET }.
///
/// Internal member for tracking command progress
///
private COMMAND command.
///
/// String to hold error messages if a command fails
///
private string error.
///
/// Get a stored error message if ExecuteCommand fails
///
public string ErrorMessage
{
get
{
return error.
}
}
///
/// bool holder for is open
///
private bool bOpen.
///
/// Check to see if a data base is open
///
public bool IsOpen
{
get
{
return bOpen.
}
}
///
/// Declare a string object for the insert command
///
public string InsertCommand
{
get
{
return dbInsertCommand.CommandText.
}
set
{
command = COMMAND.INSERT.
dbInsertCommand.CommandText = value.
}
}
///
/// Declare a string object for the 0delete command
///
public string DeleteCommand
{
get
{
return dbDeleteCommand.CommandText.
}
set
{
command = COMMAND.DELETE.
dbDeleteCommand.CommandText = value.
}
}
///
/// Declare a string object for the 0update command
///
public string UpdateCommand
{
get
{
return dbUpdateCommand.CommandText.
}
set
{
command = COMMAND.UPDATE.
dbUpdateCommand.CommandText = value.
}
}