/// <summary> /// 获取泛型实体需事务支持 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="conn"></param> /// <param name="sp"></param> /// <returns></returns> public static T SqlGetModel <T>(SqlConnection conn, SqlTransaction st, StoreProcedure sp) { try { using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = sp.ProcedureName; cmd.Transaction = st; if (sp.param != null) { cmd.Parameters.AddRange(sp.param); } using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { DataSet ds = new DataSet(); sda.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { return(DataSetModel <T>(ds, 0)); } else { return(default(T)); } } } } catch (Exception ex) { Log.Error(ex.Message, ex); return(default(T)); } finally { conn.Close(); } }
/// <summary> /// 执行单个存储过程(返回ID) /// </summary> /// <param name="conn"></param> /// <param name="st"></param> /// <param name="sp"></param> /// <returns>返回最新ID</returns> public static int ExecuteNonQuery(SqlConnection conn, SqlTransaction st, StoreProcedure sp) { try { //SqlTransaction st = connection.BeginTransaction(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.Transaction = st; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = sp.ProcedureName; cmd.Parameters.AddRange(sp.param); var dr = cmd.ExecuteReader(); cmd.Parameters.Clear(); //st.Commit(); int Id = -1; while (dr.Read()) { Id = Convert.ToInt32(dr[0]); } dr.Close(); return(Id); } catch (Exception ex) { Log.Error(ex.Message, ex); return(-1); } finally { if (st == null) { conn.Close(); } } }