///<sumary> /// 通过主键获取数据对象 ///</sumary> /// <param name="RKEY">rkey</param> ///<returns>DATA0013对象</returns> public DATA0013 getDATA0013ByRKEY(decimal rkey) { #region SQL string sql = @"select top 1 isNull(rkey,0) as rkey , isNull(np_code,'') as np_code , isNull(note_pad_line_1,'') as note_pad_line_1 , isNull(note_pad_line_2,'') as note_pad_line_2 , isNull(note_pad_line_3,'') as note_pad_line_3 , isNull(note_pad_line_4,'') as note_pad_line_4 , isNull(active_flag,0) as active_flag from DATA0013 with (nolock) where RKEY='{0}'" ; #endregion ///定义返回对象 DATA0013 data0013 = null; #region 数据库操作 try { data0013 = new DATA0013(); using (DataTable tb = dbHelper.GetDataSet(string.Format(sql, rkey))) { foreach (DataRow row in tb.Rows) { data0013.RKEY = decimal.Parse(row["RKEY"].ToString()); data0013.NP_CODE = row["NP_CODE"].ToString(); data0013.NOTE_PAD_LINE_1 = row["NOTE_PAD_LINE_1"].ToString(); data0013.NOTE_PAD_LINE_2 = row["NOTE_PAD_LINE_2"].ToString(); data0013.NOTE_PAD_LINE_3 = row["NOTE_PAD_LINE_3"].ToString(); data0013.NOTE_PAD_LINE_4 = row["NOTE_PAD_LINE_4"].ToString(); data0013.ACTIVE_FLAG = decimal.Parse(row["ACTIVE_FLAG"].ToString()); } } } catch (Exception e) { //Console.WriteLine(e.Message); log.Info("FID=" + this.factoryID.ToString() + ";userAD=" + this.userAD + ";get function:" + e.Message, e); } #endregion return(data0013); }
/// <summary> /// 向数据库中插入一条新记录。 /// </summary> /// <param name="DATA0013">data0013对象</param> /// <returns>新插入记录的编号</returns> public int Add(DATA0013 data0013) { #region 调用SQL存储过程进行添加 string sql = "sp_DATA0013_Add"; ///存储过程名 SqlParameter[] parameters = { new SqlParameter("@returnID", SqlDbType.Int), new SqlParameter("@userAD", SqlDbType.VarChar), ///new SqlParameter("@RKEY",SqlDbType.Decimal,9), new SqlParameter("@RKEY", SqlDbType.Float), new SqlParameter("@NP_CODE", SqlDbType.VarChar, 5), new SqlParameter("@NOTE_PAD_LINE_1", SqlDbType.VarChar, 70), new SqlParameter("@NOTE_PAD_LINE_2", SqlDbType.VarChar, 70), new SqlParameter("@NOTE_PAD_LINE_3", SqlDbType.VarChar, 70), new SqlParameter("@NOTE_PAD_LINE_4", SqlDbType.VarChar, 70), new SqlParameter("@ACTIVE_FLAG", SqlDbType.Decimal, 9) }; parameters[0].Value = 0; parameters[0].Direction = ParameterDirection.InputOutput; parameters[1].Value = this.userAD; parameters[2].Direction = ParameterDirection.InputOutput; parameters[2].Value = data0013.RKEY; parameters[3].Value = data0013.NP_CODE; parameters[4].Value = data0013.NOTE_PAD_LINE_1; parameters[5].Value = data0013.NOTE_PAD_LINE_2; parameters[6].Value = data0013.NOTE_PAD_LINE_3; parameters[7].Value = data0013.NOTE_PAD_LINE_4; parameters[8].Value = data0013.ACTIVE_FLAG; #endregion #region 数据库操作 int result = 0; try { dbHelper.ExecuteCommandProc(sql, parameters); result = int.Parse(parameters[0].Value.ToString()); data0013.RKEY = decimal.Parse(parameters[2].Value.ToString()); log.Info("FID=" + this.factoryID.ToString() + ";userAD=" + this.userAD + ";DATA0013,save successful"); } catch (Exception e) { ///message ID result = 2; log.Error("FID=" + this.factoryID.ToString() + ";userAD=" + this.userAD + ";" + e.Message, e); } #endregion return(result); }
///<sumary> /// 删除 ///</sumary> /// <param name="data0013">对象</param> ///<returns>返回INT类型号, 0为操作成功, 非0操作失败.</returns> public int Delete(DATA0013 data0013) { #region 调用SQL存储过程进行删除 string sql = "sp_DATA0013_Delete"; //========================= SqlParameter[] parameters = { new SqlParameter("@returnID", SqlDbType.Int), new SqlParameter("@userAD", SqlDbType.VarChar), new SqlParameter("@RKEY", SqlDbType.Decimal, 9) }; parameters[0].Value = 1; parameters[0].Direction = ParameterDirection.InputOutput; parameters[1].Value = this.userAD; parameters[2].Value = data0013.RKEY; //========================= #endregion #region 数据库操作 int result = 0; try { dbHelper.ExecuteCommandProc(sql, parameters); result = int.Parse(parameters[0].Value.ToString()); log.Info("FID=" + this.factoryID.ToString() + ";userAD=" + this.userAD + ";DATA0013,delete successful"); } catch (Exception e) { result = 2; log.Info("FID=" + this.factoryID.ToString() + ";userAD=" + this.userAD + ";" + e.Message, e); } #endregion return(result); }
public int Add(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, DATA0013 data0013) { #region 创建SQL语法 StringBuilder strSql = new StringBuilder(); strSql.Append("insert into DATA0013("); strSql.Append("NP_CODE,NOTE_PAD_LINE_1,NOTE_PAD_LINE_2,NOTE_PAD_LINE_3,NOTE_PAD_LINE_4,ACTIVE_FLAG"); strSql.Append(" ) values ("); strSql.Append("@NP_CODE,@NOTE_PAD_LINE_1,@NOTE_PAD_LINE_2,@NOTE_PAD_LINE_3,@NOTE_PAD_LINE_4,@ACTIVE_FLAG"); strSql.Append(");select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@NP_CODE", SqlDbType.VarChar, 5), new SqlParameter("@NOTE_PAD_LINE_1", SqlDbType.VarChar, 70), new SqlParameter("@NOTE_PAD_LINE_2", SqlDbType.VarChar, 70), new SqlParameter("@NOTE_PAD_LINE_3", SqlDbType.VarChar, 70), new SqlParameter("@NOTE_PAD_LINE_4", SqlDbType.VarChar, 70), new SqlParameter("@ACTIVE_FLAG", SqlDbType.Decimal, 9) }; parameters[0].Value = data0013.NP_CODE; parameters[1].Value = data0013.NOTE_PAD_LINE_1; parameters[2].Value = data0013.NOTE_PAD_LINE_2; parameters[3].Value = data0013.NOTE_PAD_LINE_3; parameters[4].Value = data0013.NOTE_PAD_LINE_4; parameters[5].Value = data0013.ACTIVE_FLAG; #endregion #region 操作 if (conn.State != ConnectionState.Open) { conn.Open(); } cmd.Connection = conn; cmd.CommandText = strSql.ToString(); cmd.Transaction = trans; cmd.CommandType = CommandType.Text;//cmdType; if (parameters != null) { foreach (SqlParameter parameter in parameters) { if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } cmd.Parameters.Add(parameter); } } int indentity = 0; object obj = cmd.ExecuteScalar(); cmd.Parameters.Clear(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { indentity = 0; } else { indentity = int.Parse(obj.ToString()); } #endregion return(indentity); }