/// <summary> /// 返回IList<KVPair>数据行集合 /// </summary> /// <param name="commandType"></param> /// <param name="commandText"></param> /// <param name="commandParams"></param> /// <returns></returns> public static IList <KVPair> ExecuteReaderToIListKVPair(CommandType commandType, string commandText, params SqlParameter[] commandParams) { using (SqlConnection conn = new SqlConnection(DbConnString_main)) { IList <KVPair> list = new List <KVPair>(); SqlCommand cmd = new SqlCommand(); SetCommand(cmd, conn, commandType, commandText, commandParams); using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { KVPair row = null; int colsLen = rdr.FieldCount; while (rdr.Read()) { row = new KVPair(); for (int i = 0; i < colsLen; i++) { row.Add(rdr.GetName(i), rdr[i].ToString()); } list.Add(row); } } cmd.Parameters.Clear(); return(list); } }
public static IList <KVPair> ExecuteDataTableToIListKVPair(string strconn, CommandType commandType, string commandText, params SqlParameter[] commandParams) { using (SqlConnection conn = new SqlConnection(strconn)) { DataTable dt = new DataTable(); SqlCommand command = new SqlCommand(); SqlDataAdapter sda = new SqlDataAdapter(); SetCommand(command, conn, commandType, commandText, commandParams); sda.SelectCommand = command; sda.Fill(dt); command.Parameters.Clear(); IList <KVPair> list = new List <KVPair>(); if (dt != null && dt.Rows.Count > 0) { int count = dt.Rows.Count; KVPair row = null; DataColumnCollection cols = dt.Rows[0].Table.Columns; int colsLen = cols.Count; for (int i = 0; i < count; i++) { row = new KVPair(); for (int k = 0; k < colsLen; k++) { row.Add(cols[k].ColumnName, dt.Rows[i][k].ToString()); } list.Add(row); } } dt.Dispose(); return(list); } }
public static KVPair ExecuteDataTableToKVPair(string dbConnString, CommandType commandType, string commandText, params SqlParameter[] commandParams) { using (SqlConnection conn = new SqlConnection(dbConnString)) { DataTable dt = new DataTable(); SqlCommand command = new SqlCommand(); SqlDataAdapter sda = new SqlDataAdapter(); SetCommand(command, conn, commandType, commandText, commandParams); sda.SelectCommand = command; sda.Fill(dt); command.Parameters.Clear(); KVPair info = null; if (dt != null && dt.Rows.Count > 0) { info = new KVPair(); DataColumnCollection cols = dt.Rows[0].Table.Columns; int colsLen = cols.Count; for (int k = 0; k < colsLen; k++) { info.Add(cols[k].ColumnName, dt.Rows[0][k].ToString()); } } dt.Dispose(); return(info); } }
public static KVPair ExecuteReaderToKVPair(string strconn, CommandType commandType, string commandText, params SqlParameter[] commandParams) { using (SqlConnection conn = new SqlConnection(strconn)) { KVPair info = null; SqlCommand cmd = new SqlCommand(); SetCommand(cmd, conn, commandType, commandText, commandParams); using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { int colsLen = rdr.FieldCount; while (rdr.Read()) { info = new KVPair(); for (int i = 0; i < colsLen; i++) { info.Add(rdr.GetName(i), rdr[i].ToString()); } } } cmd.Parameters.Clear(); return(info); } }