首页 > 其他 > 详细

通过三层实现年龄增长

时间:2015-08-11 21:13:30      阅读:218      评论:0      收藏:0      [点我收藏+]

1. 代码结果如下图所示:

????技术分享

2. TblPersonBll.cs中的代码:

????public class TblPersonBll

{

public bool IncAgeByAutoId(int autoId)

{

TblPersonDal dal=new TblPersonDal();

return dal.IncAgeByAutoId(autoId) > 0;

}

}

3. TblPersonDal.cs中的代码:

????public class TblPersonDal

{

public int IncAgeByAutoId(int autoId)

{

string sql = "update TblPerson set age=age+1 where autoId=@id";

return SqlHelper.ExcuteNonQuery(sql, CommandType.Text, new SqlParameter("@id", autoId));

}

}

4. frmIncAge.cs中的代码:

????private void button1_Click(object sender, EventArgs e)

{

TblPersonBll bll=new TblPersonBll();

int autoId = int.Parse(textBox1.Text.Trim());

if (bll.IncAgeByAutoId(autoId))

{

MessageBox.Show("ok!");

}

else

{

MessageBox.Show("not ok!");

}

}

5. SqlHelper.cs中的代码:

????public static class SqlHelper

{

//获取连接字符串

private static string conStr = ConfigurationManager.ConnectionStrings["mysqlserver"].ConnectionString;

/// <summary>

/// 执行insert/delete/update方法

/// </summary>

/// <param name="sql"></param>

/// <param name="cmdType"></param>

/// <param name="pms"></param>

/// <returns></returns>

public static int ExcuteNonQuery(string sql, CommandType cmdType, params SqlParameter[] pms)

{

using (SqlConnection con = new SqlConnection(conStr))

{

using (SqlCommand cmd = new SqlCommand(sql, con))

{

cmd.CommandType = cmdType;

if (pms != null)

{

cmd.Parameters.AddRange(pms);

}

con.Open();

return cmd.ExecuteNonQuery();

}

}

}

/// <summary>

/// 执行返回单个值方法

/// </summary>

/// <param name="sql"></param>

/// <param name="cmdType"></param>

/// <param name="pms"></param>

/// <returns></returns>

public static object ExecuteScalar(string sql, CommandType cmdType, params SqlParameter[] pms)

{

using (SqlConnection con = new SqlConnection(conStr))

{

using (SqlCommand cmd = new SqlCommand(sql, con))

{

cmd.CommandType = cmdType;

if (pms != null)

{

cmd.Parameters.AddRange(pms);

}

con.Open();

return cmd.ExecuteScalar();

}

}

?

}

/// <summary>

/// 执行返回SqlDaraReader方法

/// </summary>

/// <param name="sql"></param>

/// <param name="cmdType"></param>

/// <param name="pms"></param>

/// <returns></returns>

public static SqlDataReader ExecuteDataReader(string sql, CommandType cmdType, params SqlParameter[] pms)

{

SqlConnection con = new SqlConnection(conStr);

?

using (SqlCommand cmd = new SqlCommand(sql, con))

{

cmd.CommandType = cmdType;

if (pms != null)

{

cmd.Parameters.AddRange(pms);

}

try

{

con.Open();

return cmd.ExecuteReader(CommandBehavior.CloseConnection);

}

catch

{

con.Close();

con.Dispose();

throw;

}

}

}

?

public static DataTable ExecuteDataTable(string sql, CommandType cmdType, params SqlParameter[] pms)

{

DataTable dt = new DataTable();

using (SqlDataAdapter adapter = new SqlDataAdapter(sql, conStr))

{

adapter.SelectCommand.CommandType = cmdType;

if (pms != null)

{

adapter.SelectCommand.Parameters.AddRange(pms);

}

adapter.Fill(dt);

return dt;

}

}

?

}

通过三层实现年龄增长

原文:http://www.cnblogs.com/taidou/p/4722231.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!