增删改查
//删除
public void DeleteRanges(String activityId)
{
TransactionOptions transactionOptions = new TransactionOptions();
transactionOptions.IsolationLevel = IsolationLevel.ReadCommitted;
transactionOptions.Timeout = new TimeSpan(0, 3, 0);
using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
{
var param = new Dictionary<string, object>() { { "ActivityID", activityId } };
this._dao.ExecuteNonQuery("ZJForum.Range.delete", param);
transactionScope.Complete();
}
}
//删除时候有外键关联不能删的处理
public static string DelByID(string id)
{
try
{
int result = 0;
using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
{
Dictionary<string, string> param = new Dictionary<string, string>();
param.Add("CarID", id);
result = dao.ExecuteNonQuery("car_carinfo.delete", param);
transactionScope.Complete();
}
return string.Empty;
}
catch (Exception e)
{
if (e is System.Data.SqlClient.SqlException && ((System.Data.SqlClient.SqlException)e).Number == 547) 编号547是删除有外键关联的记录时报的错误码
{
return "该车辆有相关的使用记录,无法删除";
}
else
{
return "删除异常,请稍后再试!";
}
}
}
Aspx.cs后台代码
protected void DeleteApplication(object sender, EventArgs e)
{
ImageButton ctr = sender as ImageButton;
string delId = ctr.Attributes["_KeyID"];
string returnResult = bll.DelByID(delId);
if (string.IsNullOrEmpty(returnResult))
{
ScriptHelper.Alert(this.Page, "删除车辆成功!");
this.gvList.DataBind();
}
else
{
ScriptHelper.Alert(this.Page, returnResult);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//保存Js对象反序列话后的集合
private readonly Dao _dao = Dao.Get();
public String SaveRanges(string activityID, string rangeIds)
{
TransactionOptions transactionOptions = new TransactionOptions();
transactionOptions.IsolationLevel = IsolationLevel.ReadCommitted;
transactionOptions.Timeout = new TimeSpan(0, 3, 0);
Range range = new Range();
JavaScriptSerializer json = new JavaScriptSerializer();
List<OrgData> list = json.Deserialize<List<OrgData>>(rangeIds) == null ? new List<OrgData>() : json.Deserialize<List<OrgData>>(rangeIds);
using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
{
if (list.Count > 0)
{
foreach (var item in list)
{
range.RangeID = Guid.NewGuid().ToString();
range.ActivityID = activityID;
range.DataLocator = item.id;
this._dao.ExecuteNonQuery("ZJForum.Range.insert", range);
}
transactionScope.Complete();
}
}
return range.RangeID;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//KissU框架下的保存
private readonly Dao _dao = Dao.Get();
public String SaveOrUpdateActivity(Activity activity)
{
TransactionOptions transactionOptions = new TransactionOptions();
transactionOptions.IsolationLevel = IsolationLevel.ReadCommitted;
transactionOptions.Timeout = new TimeSpan(0, 3, 0);
using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
{
if (string.IsNullOrEmpty(activity.ActivityID))
{
activity.ActivityID = Guid.NewGuid().ToString();
this._dao.ExecuteNonQuery("ZJForum.Activity.insert", activity);
}
else
{
this._dao.ExecuteNonQuery("ZJForum.Activity.update", activity);
}
transactionScope.Complete();
}
return activity.ActivityID;
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//查询返回一个实体
public Activity GetActivity(String activityId)
{
var param = new Dictionary<string, object>() { { "ActivityID", activityId } };
return this._dao.QueryEntity<Activity>("ZJForum.Activity.get", param);
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//执行一个更新或删除
public int AnnounceActivity(string activityId)
{
try
{
var param = new Dictionary<string, object>() { { "ActivityID", activityId } };
return this._dao.ExecuteNonQuery("ZJForum.Activity.Announce", param);
}
catch (Exception ex)
{
throw ex;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//根据id查询返回一个List集合
var param = new Dictionary<string, object>();
var id = Request["ActivityID"] == null ? string.Empty : Request["ActivityID"].ToString();
param.Add("ActivityID", id);
this.Repeater1.DataSource = this._dao.QueryEntities<Range>("ZJForum.Range.list", param);
this.Repeater1.DataBind();
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//根据动软生成的代码的KISSU使用
public partial class CarInfoTotalEdit : System.Web.UI.Page
{
Car_ReportsBLL repBLL = new Car_ReportsBLL();
protected string repId { get { return this.Request["id"]; } }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(repId))
{
// 修改
SmartForm.Fill(container, repBLL.GetByID(this.Request["id"]));
}
else
{
// 新建
this.tbAddDate.Text = DateTime.Now.ToString();
// 获取最大序号
this.txtSortNum.Text = (repBLL.GetCount() + 1).ToString();
// 默认制表人
this.txtLister.Text = SecurityContext.User.Name;
}
}
}
// 保存
protected void SaveOrUpdateUser(object sender, EventArgs e)
{
Car_Reports rep = SmartForm.GetEntity<Car_Reports>(container);
repBLL.SaveOrUpdate(rep);
WebCommon.RegJs(this, "alert('保存成功!'); Close(true);");
}
}