/** Método que convierte un SQLReader a una Lista de arreglos de string List<string[]> */ public static List <string[]> SQLReaderToListArray(System.Data.SqlClient.SqlDataReader datRdr) { int tamCol = datRdr.FieldCount; string[] tupla = new string[tamCol]; List <string[]> lstRes = new List <string[]>(); System.Data.DataTable schemaTable = datRdr.GetSchemaTable(); System.Data.DataRow datRow; for (int i = 0; i < schemaTable.Rows.Count; i++) { datRow = schemaTable.Rows[i]; tupla[i] = "" + datRow[0]; } lstRes.Add(tupla); while (datRdr.Read()) { tupla = new string[tamCol]; for (int i = 0; i < tamCol; i++) { tupla[i] = datRdr[i].ToString(); } lstRes.Add(tupla); } return(lstRes); }
private DataTable CreateSchemaDataTable(System.Data.SqlClient.SqlDataReader reader) { if (reader == null) { return(null); } if (reader.IsClosed) { return(null); } DataTable schema = reader.GetSchemaTable(); DataTable dt = new DataTable(); foreach (DataRow row in schema.Rows) { // Column情報を追加してください。 DataColumn col = new DataColumn(); col.ColumnName = row["ColumnName"].ToString(); col.DataType = Type.GetType(row["DataType"].ToString()); if (col.DataType.Equals(typeof(string))) { col.MaxLength = (int)row["ColumnSize"]; } dt.Columns.Add(col); } return(dt); }
public DataSet FromIRecordReader(IRecordsReader recordsReader) { // Get the internal RawDataReader (obsolete) System.Data.SqlClient.SqlDataReader reader = ((umbraco.DataLayer.SqlHelpers.SqlServer.SqlServerDataReader)recordsReader).RawDataReader; DataSet dataSet = new DataSet(); do { DataTable dataTable = new DataTable(); DataTable schemaTable = reader.GetSchemaTable(); if (schemaTable != null) { // A query returning records was executed foreach (DataRow dataRow in schemaTable.Rows) { // Create a column name that is unique in the data table string columnName = (string)dataRow["ColumnName"]; // Add the column definition to the data table DataColumn column = new DataColumn(columnName, (Type)dataRow["DataType"]); dataTable.Columns.Add(column); } dataSet.Tables.Add(dataTable); // Fill the data table we just created while (reader.Read()) { DataRow dataRow = dataTable.NewRow(); for (int i = 0; i < reader.FieldCount; i++) { dataRow[i] = reader.GetValue(i); } dataTable.Rows.Add(dataRow); } } else { // No records were returned, return number of rows affected dataTable.Columns.Add(new DataColumn("RowsAffected")); dataSet.Tables.Add(dataTable); DataRow rowsAffectedRow = dataTable.NewRow(); rowsAffectedRow[0] = reader.RecordsAffected; dataTable.Rows.Add(rowsAffectedRow); } } // Go trough all result sets while (reader.NextResult()); // Close the data reader so the underlying connection is closed recordsReader.Close(); return(dataSet); }
private static object GetActualValue(System.Data.SqlClient.SqlDataReader actual, string colName) { var providerType = actual.GetSchemaTable().AsEnumerable() .Where(x => string.Compare(x["ColumnName"].ToString(), colName, true) == 0) .Select(x => (SqlDbType)(int)x["ProviderType"]).FirstOrDefault(); if (providerType == SqlDbType.Date && actual[colName] != DBNull.Value) { return(((DateTime)actual[colName]).ToShortDateString()); } return(actual[colName]); }
public List <T> Select(string selectSql) { List <T> list = new List <T>(); using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(CacheConnectionString)) { using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(selectSql, conn)) { System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader(); System.Data.DataTable dt = reader.GetSchemaTable(); System.Type type = typeof(T); // 获得此模型的类型 string tempName = ""; foreach (System.Data.DataRow dr in dt.Rows) { T t = new T(); System.Reflection.PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性 foreach (System.Reflection.PropertyInfo pi in propertys) { tempName = pi.Name; if (dt.Columns.Contains(tempName)) { if (!pi.CanWrite) { continue; } object value = dr[tempName]; if (value != System.DBNull.Value) { pi.SetValue(t, value, null); } } } list.Add(t); } return(list); } } }
void StreamData() { _waitHandle.WaitOne(); while (_stream.HasRows) { DataTable resultSet = _stream.GetSchemaTable(); object[] values = new object[_stream.FieldCount - 1]; while (_stream.Read()) { _stream.GetValues(values); resultSet.Rows.Add(values); _waitHandle.WaitOne(100); } _dataReady = true; _resultSets.Add(resultSet); _stream.NextResult(); } _dataReady = true; }