/// <summary> ///Save,保存方法,先删除,再增加 /// </summary> public void Save(SCM_Item model, SqlTransaction tran = null, params string[] str) { string tran_flag = "1"; SqlConnection conn = new SqlConnection(Dal.DataHelper.constr); conn.Open(); if (tran == null) { tran_flag = "0"; tran = conn.BeginTransaction(); } try { Delete(model, tran, str); Add(model, tran); //如果传入事物,提交否则外层提交 if (tran_flag == "0") { tran.Commit(); conn.Close(); } } catch (Exception e) { if (tran_flag == "0") { tran.Rollback(); conn.Close(); } throw e; } }
/// <summary> /// 增加一条数据 /// </summary> public override void Add(dynamic obj, SqlTransaction tran = null) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into SCM_Item("); strSql.Append("cInvCode,cFree9,cFree10,PartId,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8"); strSql.Append(") values ("); strSql.Append("@cInvCode,@cFree9,@cFree10,@PartId,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8"); strSql.Append(") "); SqlParameter[] parameters = { new SqlParameter("@cInvCode", SqlDbType.NVarChar, 60), new SqlParameter("@cFree9", SqlDbType.NVarChar, 20), new SqlParameter("@cFree10", SqlDbType.NVarChar, 20), new SqlParameter("@PartId", SqlDbType.Int, 4), new SqlParameter("@cFree1", SqlDbType.NVarChar, 20), new SqlParameter("@cFree2", SqlDbType.NVarChar, 20), new SqlParameter("@cFree3", SqlDbType.NVarChar, 20), new SqlParameter("@cFree4", SqlDbType.NVarChar, 20), new SqlParameter("@cFree5", SqlDbType.NVarChar, 20), new SqlParameter("@cFree6", SqlDbType.NVarChar, 20), new SqlParameter("@cFree7", SqlDbType.NVarChar, 20), new SqlParameter("@cFree8", SqlDbType.NVarChar, 20) }; SCM_Item model = (SCM_Item)obj; parameters[0].Value = SqlNull(model.cInvCode); parameters[1].Value = SqlNull(model.cFree9); parameters[2].Value = SqlNull(model.cFree10); parameters[3].Value = SqlNull(model.PartId); parameters[4].Value = SqlNull(model.cFree1); parameters[5].Value = SqlNull(model.cFree2); parameters[6].Value = SqlNull(model.cFree3); parameters[7].Value = SqlNull(model.cFree4); parameters[8].Value = SqlNull(model.cFree5); parameters[9].Value = SqlNull(model.cFree6); parameters[10].Value = SqlNull(model.cFree7); parameters[11].Value = SqlNull(model.cFree8); if (tran == null) { DataHelper.ExcuteNonQuery(strSql.ToString(), parameters, false); } else { DataHelper.ExcuteNonQuery(strSql.ToString(), tran, parameters, false); } }