首页  ·  知识 ·  编程语言
三层架构之数据库访问层完全篇(C#)
佚名  http://blog.csdn.net/moonshineidolon/archive/2007/  .NET  编辑:dezai  图片来源:网络
三层架构之数据库访问层完全篇(C#) using System; using System.Data; using System.Data.SqlClient; using System.Configuratio

三层架构之数据库访问层完全篇(C#)
 
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DbBase
...{

public abstract class Base
...{

"Fields of base calss"#region "Fields of base calss"

protected static string strConn = ConfigurationSettings.AppSettings["strConnection"];

protected static string strSQL;

#endregion


"Properties of base class"#region "Properties of base class"
}

#endregion


"Functions of base class"#region "Functions of base class"
public Base()
...{
//
// TODO: Add constructor logic here
//
}

/**////


/// executing SQL commands
///

/// string
/// return int
protected static int ExecuteSql(string strSQL)
...{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
...{
myCn.Open();
myCmd.ExecuteNonQuery();
return 0;
}
catch(System.Data.SqlClient.SqlException e)
...{
throw new Exception(e.Message);
}
finally
...{
myCmd.Dispose();
myCn.Close();
}
}


/**////


///executing SQL commands
///

/// 要执行的SQL语句,为字符串类型string
/// 返回执行情况,整形int
protected static int ExecuteSqlEx(string strSQL)
...{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);

try
...{
myCn.Open();
SqlDataReader myReader = myCmd.ExecuteReader();
if(myReader.Read())
...{
return 0;
}
else
...{
throw new Exception("Value Unavailable!");
}
}
catch(System.Data.SqlClient.SqlException e)
...{
throw new Exception(e.Message);
}
finally
...{
myCmd.Dispose();
myCn.Close();
}
}


/**////


/// get dataset
///

/// (string)
/// (DataSet)
protected static DataSet ExecuteSql4Ds(string strSQL)
...{
SqlConnection myCn = new SqlConnection(strConn);
try
...{
myCn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strSQL,myCn);
DataSet ds = new DataSet("ds");
sda.Fill(ds);
return ds;
}
catch(System.Data.SqlClient.SqlException e)
...{
throw new Exception(e.Message);
}
finally
...{
myCn.Close();
}
}


/**////


/// get single value
///

/// (string)
/// (int)
protected static int ExecuteSql4Value(string strSQL)
...{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
...{
myCn.Open();
object r = myCmd.ExecuteScalar();
if(Object.Equals(r,null))
...{
throw new Exception("value unavailable!");
}
else
...{
return (int)r;
}
}
catch(System.Data.SqlClient.SqlException e)
...{
throw new Exception(e.Message);
}
finally
...{
myCmd.Dispose();
myCn.Close();
}
}


/**////


/// get object
///

/// (string)
/// (object)
protected static object ExecuteSql4ValueEx(string strSQL)
...{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
...{
myCn.Open();
object r = myCmd.ExecuteScalar();
if(Object.Equals(r,null))
...{
throw new Exception("object unavailable!");
}
else
...{
return r;
}
}
catch(System.Data.SqlClient.SqlException e)
...{
throw new Exception(e.Message);
}
finally
...{
myCmd.Dispose();
myCn.Close();
}
}


/**////


/// execute multipul SQL commands
///

/// string
/// int
protected static int ExecuteSqls(string[] strSQLs)
...{
SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand();
int j=strSQLs.Length;

try
...{
myCn.Open();
}
catch(System.Data.SqlClient.SqlException e)
...{
throw new Exception(e.Message);
}
SqlTransaction myTrans = myCn.BeginTransaction();

try
...{
myCmd.Connection = myCn;
myCmd.Transaction = myTrans;

foreach(string str in strSQLs)
...{
myCmd.CommandText = str;
myCmd.ExecuteNonQuery();
}
myTrans.Commit();
return 0;
}
catch(System.Data.SqlClient.SqlException e)
...{
myTrans.Rollback();
throw new Exception(e.Message);
}
finally
...{
myCmd.Dispose();
myCn.Close();
}
}

#endregion
}
}

本文作者:佚名 来源:http://blog.csdn.net/moonshineidolon/archive/2007/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读