class SqlHelper
{
private static readonly string connStr = ConfigurationManager.ConnectionStrings["testConn"].ConnectionString;
public static SqlConnection createConnection()
{
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
return conn;
}
//执行操作:增、删、改
public static int ExecuteNoneQuery(SqlConnection conn, string sql, params SqlParameter[] parameters)
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);//添加SqlParameters数组
return cmd.ExecuteNonQuery();//返回
}
}
public static int ExecuteNoneQuery(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = createConnection())
{
return ExecuteNoneQuery(conn, sql, parameters);
}
}
}
public class StudentDAL
{
public int Add(string Name, string Birthday, string Gender, int? Tall, int? Weight, string Address)
{
object obj = SqlHelper.ExecuteScalar("insert into Student values(@Name,@Birthday,@Gender,@Tall,@Weight,@Address)",
new SqlParameter("@Name", Name),
new SqlParameter("@Birthday", Birthday),
new SqlParameter("@Gender", Gender),
new SqlParameter("@Tall", Tall == null ? (object)DBNull.Value : Tall),
new SqlParameter("@Weight", Weight ?? (object)DBNull.Value),
new SqlParameter("@Address", Address == null ? (object)DBNull.Value : Address));
return Convert.ToInt32(obj);
}
}
第3步,新建类库文件(BLL层),名字叫Student.BLL。添加对Student.DAL的引用,然后新建StudentBLL类,详细代码如下(注意要using Student.DAL;):
public class StudentBLL
{
private StudentDAL sDal = new StudentDAL();
public int Add(string Name, string Birthday, string Gender, int? Tall, int? Weight, string Address)
{
return sDal.Add(Name, Birthday, Gender, Tall, Weight, Address);
}
}
第4步,新建窗体应用程序(UI层),名字叫Student.Winform。添加对Student.BLL的引用,然后添加一个Button按钮控件,并注册一个单击事件,后台代码如下(注意要using Student.BLL;):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
StudentBLL bll = new StudentBLL();
int num = bll.Add("stu6", "2014-12-25", "男", 120, 100, "北京市中关村");
int num2 = bll.Add("stu7", "2017-10-12", "男", 130, 110, "中石油小区");
}
}
第5步,在窗体应用程序的App.config中2)对于上面的练习,如果在增加两条记录的中途出现了异常会怎样
现在对上面代码做一些修改,使得第一句sql执行成功后就出现异常,故意让第二句sql无法得到执行。在按钮单击事件里添加如下图所示的一句代码:
private void button1_Click(object sender, EventArgs e)
{
StudentBLL bll = new StudentBLL();
using (TransactionScope tx = new TransactionScope())//事务处理
{
int num = bll.Add("stu9", "2014-12-25", "男", 120, 100, "北京市中关村");
Convert.ToInt32("abc");//人为地制造一个异常
int num2 = bll.Add("stu10", "2017-10-12", "男", 130, 110, "中石油小区");
tx.Complete();
}
}
第3步,将sql操作包裹到TransactionScope中,这样就可以被事务所监控。当其中出现异常时,让所有修改过的数据回滚到修改之前。
2.如何看待“事务不是属于DAL层的吗,为什么放到UI层处理?”这个问题。实际上,架构只是为了更好解决问题而存在,并不是约束程序员。如果某些方式更加方便,那么违反三层架构也是可以的。