using System; using System.Data; using System.Data.SqlClient; using System.Collections; using System.Collections.Generic; using DSWeb.Models; using WebSqlHelper; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Text; namespace DSWeb.EntityDA { public class CrmKeyCodeSetDA { /// /// 是否存在该条数据 /// public bool Exists(string GID) { using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction)) { StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) from crm_key_code"); strSql.Append(" where "); strSql.Append(" ID = @ID and KEYVALUE<>'' and KEYVALUE is not null"); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int,4)}; parameters[0].Value = GID; try { return bool.Parse(SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql.ToString(), parameters).ToString()); } catch { return false; } } } /// /// 增加一条数据 /// public int Add(CrmKeyCodeSetEntity model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into crm_key_code(ID,KEYTYPE,KEYVALUE) values (@ID,@KEYTYPE,@KEYVALUE) "); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.VarChar,36) , new SqlParameter("@KEYTYPE", SqlDbType.VarChar,50) , new SqlParameter("@KEYVALUE", SqlDbType.VarChar,100) }; parameters[0].Value = model.ID; parameters[1].Value = model.KEYTYPE; parameters[2].Value = model.KEYVALUE; // int iResult = 0; using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction)) { int existVal = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql.ToString(), parameters); if (existVal > 0) { iResult = 1; // //string str0 = "insert into sys_log(NAME,LOGTYPE,LOGCONTENT,CREATEUSER) values('增加信息','增加操作','GID=" + model.GID.ToString() + "','" + model.MODIFIEDUSER.ToString() + "')"; //bool bl0 = SqlHelper.ExecuteSqlCommand(SqlHelper.ConnectionStringLocalTransaction, str0); } else { iResult = -1;//执行异常 } } return iResult; } /// /// 更新一条数据 /// public int Update(CrmKeyCodeSetEntity model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update crm_key_code set "); strSql.Append(" KEYTYPE = @KEYTYPE , "); strSql.Append(" KEYVALUE = @KEYVALUE "); strSql.Append(" where ID=@ID "); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.VarChar,36) , new SqlParameter("@KEYTYPE", SqlDbType.VarChar,50) , new SqlParameter("@KEYVALUE", SqlDbType.VarChar,100) }; parameters[0].Value = model.ID; parameters[1].Value = model.KEYTYPE; parameters[2].Value = model.KEYVALUE; // int iResult = 0; using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction)) { int existVal = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql.ToString(), parameters); if (existVal > 0) { iResult = 1; // //string str0 = "insert into sys_log(NAME,LOGTYPE,LOGCONTENT,CREATEUSER) values('更新信息表','更新操作','GID=" + model.GID.ToString() + "','" + model.MODIFIEDUSER.ToString() + "')"; //bool bl0 = SqlHelper.ExecuteSqlCommand(SqlHelper.ConnectionStringLocalTransaction, str0); } else { iResult = -1;//执行异常 } } return iResult; } /// /// 删除一条数据 /// public int Delete(int ID) { StringBuilder strSql = new StringBuilder(); strSql.Append("delete from crm_key_code "); strSql.Append(" where ID=@ID and KEYVALUE<>'' and KEYVALUE is not null"); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int,4) }; parameters[0].Value = ID; // int iResult = 0; using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction)) { try { SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql.ToString(), parameters); iResult = 1;//状态为1表示删除成功 sqlTran.Commit(); } catch (Exception execError) { iResult = -1;//有异常,删除失败 sqlTran.Rollback(); iResult = -2;//插入异常,事务已回滚成功 throw execError; } finally { SqlHelper.CloseConnection(); } } return iResult; } /// /// 批量删除一批数据 /// public bool DeleteList(string IDlist) { StringBuilder strSql = new StringBuilder(); strSql.Append("delete from crm_key_code "); strSql.Append(" where ID in (" + IDlist + ") and KEYVALUE<>'' and KEYVALUE is not null"); bool bl = SqlHelper.ExecuteSqlCommand(SqlHelper.ConnectionStringLocalTransaction, strSql.ToString()); return bl; } #region 返回所有用户数据集 /// /// 返回所有用户数据集 /// /// 查询SQL语句 /// public DataSet GetExcuteSql(string strSql) { DataSet userSet = new DataSet(); userSet = SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql); return userSet; } #endregion /// /// 得到一个对象实体 /// public CrmKeyCodeSetEntity GetModel(int ID) { StringBuilder strSql = new StringBuilder(); strSql.Append("select ID, KEYTYPE, KEYVALUE "); strSql.Append(" from crm_key_code "); strSql.Append(" where ID=@ID and KEYVALUE<>'' and KEYVALUE is not null"); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int,4) }; parameters[0].Value = ID; CrmKeyCodeSetEntity model = new CrmKeyCodeSetEntity(); DataSet ds = SqlHelper.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { model.ID = ds.Tables[0].Rows[0]["ID"].ToString(); model.KEYTYPE = ds.Tables[0].Rows[0]["KEYTYPE"].ToString(); model.KEYVALUE = ds.Tables[0].Rows[0]["KEYVALUE"].ToString(); return model; } else { return null; } } /// /// 增加一条数据 /// public int AddAll(IList KeyEntities) { int iResult = 0; using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction)) { try { foreach (CrmKeyCodeSetEntity model in KeyEntities) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into crm_key_code(ID,KEYTYPE,KEYVALUE) values (@ID,@KEYTYPE,@KEYVALUE)"); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.VarChar,36) , new SqlParameter("@KEYTYPE", SqlDbType.VarChar,50) , new SqlParameter("@KEYVALUE", SqlDbType.VarChar,100) }; parameters[0].Value = model.ID; parameters[1].Value = model.KEYTYPE; parameters[2].Value = model.KEYVALUE; // iResult = -3;// SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, strSql.ToString(), parameters); } //事务提交 sqlTran.Commit(); iResult = 1;//状态为1表示插入成功 } catch (Exception execError) { iResult = -1;//有异常,插入失败 sqlTran.Rollback(); iResult = -2;//插入异常,事务已回滚成功 throw execError; } finally { SqlHelper.CloseConnection(); } } return iResult; } /// /// 更新一条数据 /// public int UpdateAll(IList KeyEntities) { int iResult = 0; using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction)) { try { T_ALL_DA T_ALL_DA = new T_ALL_DA(); foreach (CrmKeyCodeSetEntity model in KeyEntities) { string ls = ""; if (model.ID == null) { model.ID = Guid.NewGuid().ToString(); } else { object statusObj = SqlHelper.ExecuteScalar(sqlTran, CommandType.Text, "select id from crm_key_code where id='" + model.ID.ToString().Trim() + "'", null); if (statusObj == null) { ls = ""; } else { ls = statusObj.ToString().Trim(); } } if (ls.Trim() == "") { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into crm_key_code(ID,KEYTYPE,KEYVALUE) values (@ID,@KEYTYPE,@KEYVALUE)"); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.VarChar,36) , new SqlParameter("@KEYTYPE", SqlDbType.VarChar,50) , new SqlParameter("@KEYVALUE", SqlDbType.VarChar,100) }; parameters[0].Value = model.ID; parameters[1].Value = model.KEYTYPE; parameters[2].Value = model.KEYVALUE; // iResult = -3;// SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, strSql.ToString(), parameters); } else { StringBuilder strSql = new StringBuilder(); strSql.Append("update crm_key_code set "); strSql.Append(" KEYTYPE = @KEYTYPE , "); strSql.Append(" KEYVALUE = @KEYVALUE "); strSql.Append(" where ID=@ID "); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.VarChar,36) , new SqlParameter("@KEYTYPE", SqlDbType.VarChar,50) , new SqlParameter("@KEYVALUE", SqlDbType.VarChar,100) }; parameters[0].Value = model.ID; parameters[1].Value = model.KEYTYPE; parameters[2].Value = model.KEYVALUE; // iResult = -5;// SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, strSql.ToString(), parameters); } } //事务提交 sqlTran.Commit(); iResult = 1;//状态为1表示插入成功 } catch (Exception execError) { iResult = -1;//有异常,插入失败 sqlTran.Rollback(); iResult = -2;//插入异常,事务已回滚成功 throw execError; } finally { SqlHelper.CloseConnection(); } } return iResult; } } }