Created
October 23, 2017 21:18
-
-
Save jabez007/31068ea4278b166d902d465359cefca1 to your computer and use it in GitHub Desktop.
Generic SqlDataReader to List<OrderedDictionary>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static List<OrderedDictionary> TrySqlQuery(string sqlQuery) | |
{ | |
List<OrderedDictionary> sqlResults = new List<OrderedDictionary>(); | |
using (SqlConnection conn = new SqlConnection(connectionString)) | |
{ | |
try | |
{ | |
conn.Open(); | |
using (SqlCommand _cmd = new SqlCommand(sqlQuery, conn)) | |
{ | |
SqlDataReader _reader = _cmd.ExecuteReader(); | |
string columnName; | |
OrderedDictionary row; | |
while (_reader.Read()) | |
{ | |
row = new OrderedDictionary(); | |
for (int i = 0; i < _reader.FieldCount; i++) | |
{ | |
columnName = _reader.GetName(i); | |
row.Add(columnName, _reader[columnName]); | |
} | |
sqlResults.Add(row); | |
} | |
} | |
} | |
catch (SqlException e) | |
{ | |
} | |
} | |
return sqlResults; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment