public static DataTable GetDataTableFromReader(System.Data.SqlClient.SqlDataReader DataReader) { DataTable newTable = new DataTable(); DataColumn Col; DataRow Row; for (int i = 0; i < DataReader.FieldCount - 1; i++) { Col = new DataColumn(); Col.ColumnName = DataReader.GetName(i); Col.DataType = DataReader.GetFieldType(i); newTable.Columns.Add(Col); } while (DataReader.Read()) { Row = newTable.NewRow(); for (int i = 0; i < DataReader.FieldCount - 1; i++) { Row[i] = DataReader[i]; } newTable.Rows.Add(Row); } return(newTable); }
/// <summary> /// DataReader转换为Json /// </summary> /// <param name="dataReader">DataReader对象</param> /// <returns>Json字符串(数组)</returns> /// public static string DataReaderToJson(System.Data.SqlClient.SqlDataReader dataReader) { StringBuilder jsonString = new StringBuilder(); jsonString.Append("["); while (dataReader.Read()) { jsonString.Append("{"); for (int i = 0; i < dataReader.FieldCount; i++) { Type type = dataReader.GetFieldType(i); string strKey = dataReader.GetName(i); string strValue = dataReader[i].ToString(); jsonString.Append("\"" + strKey + "\":"); strValue = String.Format(strValue, type); //datetime不能出现为空的情况,所以将其转换成字符串来进行处理。 //需要加""的 if (type == typeof(string) || type == typeof(DateTime)) { if (i <= dataReader.FieldCount - 1) { jsonString.Append("\"" + strValue + "\","); } else { jsonString.Append(strValue); } } //不需要加""的 else { if (i <= dataReader.FieldCount - 1) { jsonString.Append("" + strValue + ","); } else { jsonString.Append(strValue); } } } jsonString.Append("},"); } dataReader.Close(); jsonString.Remove(jsonString.Length - 3, 3); jsonString.Append("}"); jsonString.Append("]"); return(jsonString.ToString()); }
private async Task readFromDataReader(TNDMetaEntity metaEntity, List <TNDEntityItem> result, System.Data.SqlClient.SqlDataReader sqlDataReader) { // loop over NextResult bool next = true; while (next) { var fieldCount = sqlDataReader.FieldCount; string[] names = new string[fieldCount]; Type[] types = new Type[fieldCount]; TNDPropertyCache[] caches = new TNDPropertyCache[fieldCount]; object[] values = new object[fieldCount]; int idxMetaEntityName = -1; for (int idx = 0; idx < fieldCount; idx++) { names[idx] = sqlDataReader.GetName(idx); types[idx] = sqlDataReader.GetFieldType(idx); caches[idx] = new TNDPropertyCache(); if (string.Equals(names[idx], "MetaEntityName", StringComparison.Ordinal)) { idxMetaEntityName = idx; } } // loop over Read bool read = true; while (read) { var taskReadAsync = sqlDataReader.ReadAsync(); try { read = await taskReadAsync; } catch (Exception exception) { throw; } if (read) { sqlDataReader.GetValues(values); var item = new TNDEntityItem(); for (int idx = 0; idx < fieldCount; idx++) { if (idx == idxMetaEntityName) { item.MetaEntityName = values[idx] as string; } else { var property = new TNDProperty(names[idx], types[idx], values[idx]); if (caches[idx] != null) { property = caches[idx].GetOrCreate(property); } item.Property.Add(property); } } if (item.MetaEntityName != null) { item.MetaEntity = this._ApplicationBuis.ApplicationModel.Configuration.FindMetaEntity(item.MetaEntityName); } if (item.MetaEntity == null) { item.MetaEntity = metaEntity; item.MetaEntityName = metaEntity.MetaEntityName; } result.Add(item); } } var taskNextResult = sqlDataReader.NextResultAsync(); try { next = await taskNextResult; } catch (Exception exception) { throw; } } }