#region 获取DataSet返回结构集 public DataSet GetDataSet(string sql, params SqlParameter[] para) { for (int i = 0; i < para.Length; i++) { SqlParameter p = para[i]; sql += " " + p.ParameterName + ","; } sql = sql.Substring(0, sql.Length - 1); DataSet sd = new DataSet(); using (var db = new BaseContext()) { SqlConnection conn = db.Database.Connection as SqlConnection; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddRange(para); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(sd); } } return sd; } #endregion #region 获取Table返回DataTable public DataTable GetDataTable(string sql, params SqlParameter[] para) { DataTable dt=new DataTable(); for (int i = 0; i < para.Length; i++) { SqlParameter p = para[i]; sql += " " + p.ParameterName + ","; } sql = sql.Substring(0, sql.Length - 1); using (var db = new BaseContext()) { SqlConnection conn = db.Database.Connection as SqlConnection; using (SqlCommand cmd=new SqlCommand(sql,conn)) { cmd.Parameters.AddRange(para); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); return dt; } } } #endregion
时间: 2024-12-17 22:11:29