在多层开发中通常会有一个公共基类,用来构造、执行各种带有共性的数据库底层操作,参考了一些高手这样的基类代码,觉得很多地方值得学习,对于我这样的新手,不论是对多层开发的理解、对ADO.NET的理解、还是对代码效率的提高都有非常大的作用,所以特抛砖引“码”,望各星星、三角们帖出一些供大家学习。
下面是我这段时间按自己的项目需求胡编乱造出的一个公共基类的部分(基于存储过程),因项目未完成所以这个基类也就没写完,但总觉得写得不标准也不太科学,想找些参考找来找去也找不着:
public abstract class DBAccess
{
//数据库连接字符串
private string connectionstring;
//SqlConnection对象私有字段
private SqlConnection conn;
//SqlCommand对象
private SqlCommand cmd = new SqlCommand();
//DataAdapter对象
private SqlDataAdapter da = new SqlDataAdapter();
//DataSet对象
private DataSet ds = new DataSet();
//DataTable对象
private DataTable dt = new DataTable();

#region 构造函数
public  DBAccess(string input_connectionstring)
{
connectionstring = input_connectionstring;
conn = new SqlConnection(connectionstring);
}
#endregion #region *无参数存储过程创建SqlCommand对象
public SqlCommand BuildSqlCommand (string procedure_name)
{
try
{
cmd = new SqlCommand(procedure_name , conn);
cmd.CommandType = CommandType.StoredProcedure;
return cmd;
}
catch (Exception error)
{
throw error;
}
}
#endregion #region 有参数存储过程创建SqlCommand对象
public SqlCommand BuildSqlCommand (string procedure_name , IDataParameter[] parameters)
{
try
{
cmd = new SqlCommand (procedure_name , conn);
cmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
cmd.Parameters.Add (parameter);
}
return cmd;
}
catch (Exception error)
{
throw error;
}
}
#endregion #region 运行查询存储过程(例:SELECT COUNT(*) FROM table_name格式查询),返回行数
public int RunSelectProcedure (string procedure_name , IDataParameter[] parameters)
{
int rows_count;
try
{
if(conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd = this.BuildSqlCommand(procedure_name , parameters);
rows_count = (int)cmd.ExecuteScalar();
return rows_count;
}
catch(Exception error)
{
throw error;
}
finally
{
conn.Close();
}
}
#endregion #region 运行存储过程(INSERT、UPDATE、DELETE),返回成功行数
public int RunProcedure (string procedure_name , IDataParameter[] parameters)
{
int rows_count;
try
{
if(conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd = this.BuildSqlCommand(procedure_name , parameters);
rows_count = cmd.ExecuteNonQuery();
return rows_count;
}
catch(Exception error)
{
throw error;
}
finally
{
conn.Close();
}
}
#endregion #region 运行无参数存储过程(SELECT查询),返回一个DataTable
public DataTable RunProcedure (string procedure_name , string table_name)
{
try
{
if(conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd = this.BuildSqlCommand(procedure_name);
da = new SqlDataAdapter(cmd);
da.Fill(ds,table_name);
return ds.Tables[table_name];
}
catch(Exception error)
{
throw error;
}
finally
{
conn.Close();
}
}
#endregion #region 为DataTable增加序号列(true:递增,false:递减),返回新的DataTable
public DataTable AddNumber(DataTable table_object_name,string column_name,bool direction)
{
try
{
dt = table_object_name;
//增加列
dt.Columns.Add(column_name,System.Type.GetType("System.Int32"));
if(direction == true)
{
//递增赋值
for(int i=0;i<dt.Rows.Count;i++)
{
dt.Rows[i][column_name] = dt.Rows.Count-i;
}
return dt;
}
else
{
//递减赋值
for(int i=dt.Rows.Count;i>0;i--)
{
dt.Rows[i-1][column_name] = i;
}
return dt;
}
}
catch(Exception error)
{
throw error;
}
}
#endregion }

解决方案 »

  1.   

    欢迎加入群 asp.net+c# 群号是:7199731
    本人用asp.net+c#仿照chinaren做的一个校友录系统,具有chinaren校友录的绝大部分功能!大家看看! 地址是:http://219.229.249.5/txl/default.aspx
    采用自己电脑做服务器。速度可能很慢,也可能访问不了!
    用guest登陆 密码也是guest!
      

  2.   

    asp.net实例 1.0的源代码里有一个,楼主可以参考。楼主在google.com搜索嘛。
      

  3.   

    [原创] Asp.net三层架构实例V1.0   http://www.aspxcn.org/Modules/Sys/Default.aspx?Parent_Id=4&Url=