数据库操作类实现(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.
}
}

相关文章


最新Dev-c 与Allegor平台的搭建方法
编程初学者易犯的毛病
学习C语言艺术清屏
C#实现SQLServer数据库日志的配置方法
数据库操作类实现(C#,SqlClient)
C#获取数据库表的创建脚本
C#备份和恢复SQLServer数据库
C#中对XML文件的基本操作
c语言下的工厂模式——ipmi源码分析
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛