Read data from SQL Server with dictionary in C# - c#

var query = "SELECT OrderNo, PlannedQuantity, CompletedQuantity, IsClosed" +
" FROM Order1 WHERE OrderNo =:orderNo";
var arguments = new Dictionary<string, object>() { { "orderNo", orderNo } };
using (var reader = ExecuteDatabaseReader(query, arguments))
{
if (reader.Read())
{
return GetOrderFromReader(reader);
}
}
static IDataReader ExecuteDatabaseReader(string query, IDictionary<string, object> arguments)
{
return null;
}

You should implement a method like below. Of cource code below is a simple code and it needs to be very better. For example, I haven't managed to close and dispose the connection.
static IDataReader ExecuteDatabaseReader(string query, IDictionary < string, object > arguments) {
var conn = new SqlConnection(your_connection_string);
conn.Open();
var commadn = conn.CreateCommand();
command.CommandText = query;
foreach(var key in arguments.Keys) {
command.Parameters.AddWithValue(key, arguments[key]);
}
return command.ExecuteReader();
}
One important notes about parameters, I don't know what your RDBMS is if it's SQL Server you need to define your parameters with # sign.
var dictionary = new Dictionary<string, object>();
dictionary.Add("#Id", 1);
dictionary.Add("#Name", "Name");

Related

Read data from DB generic function

I am new to working in dot net. What I am trying to do is create a helper method which will fetch data from DB for any table. I have passed parameters dynamically it working fine. But when I try to read the data, I am finding it difficult to store the data in some collection. This I will be returning back to my calling point and bind it to a response type and return.
public static Dictionary<Dictionary<string, object>, object> GetData(SqlCommand cmd, string connectionString, List<SqlParameter> parameters)
{
try
{
SqlDataReader reader = null;
Dictionary<Dictionary<string,object>, object> returnObjects = new Dictionary<Dictionary<string, object>, object>();
Dictionary<string, object> returnObject = new Dictionary<string, object>();
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
cmd.Connection = sqlConnection;
cmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
cmd.Parameters.Add(parameter);
}
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
returnObject.Add(reader.GetName(i), reader[i]);
}
returnObject.Clear();
returnObjects.Add(returnObject, reader);
}
}
}
return returnObjects;
}
catch (Exception ex)
{
throw;
}
}
In above code when i try to add in returnObjects dictionary it says key already added. Below is the code from where I am calling and patientResponse where I want to return.
Dictionary<Dictionary<string, object>, object> dct = new Dictionary<Dictionary<string, object>, object>();
dct = Helper.GetData(cmd, connectionString, parameters);
List<Patient_Response> pp = new List<Patient_Response>();
Patient_Response pr = new Patient_Response();
pr.Patient_Id = int.Parse(reader["ID"].ToString());
pr.FIRST_NAME = reader["FIRST_NAME"].ToString();
pr.LAST_NAME = reader["LAST_NAME"].ToString();
pr.phoneNumber = reader["TEL"].ToString();
pr.email = reader["EMAIL"].ToString();
pr.Address = reader["Address"].ToString();
pr.Gender = reader["Gender"].ToString();
pr.DOB = Convert.ToDateTime(reader["DOB"]).ToString("MM/dd/yyyy");
pp.Add(pr);
What i can use instead of dictionary so that i can get a collection returned.
What surely doesn't work in your code is that you add the same instance of returnObject for each row. You should put new Dictionary<string, object>() inside the while. Also, if your result set contains duplicate field names, you'll get an exception.
public static List<Dictionary<string, object>> GetData(SqlCommand cmd, string connectionString, List<SqlParameter> parameters)
{
try
{
SqlDataReader reader = null;
// changed this to be a list
List<Dictionary<string,object>> returnObjects = new List<Dictionary<string, object>>();
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
cmd.Connection = sqlConnection;
cmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
cmd.Parameters.Add(parameter);
}
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
// create a new returnObject for each row
Dictionary<string, object> returnObject = new Dictionary<string, object>();
for (int i = 0; i < reader.FieldCount; i++)
{
// following line throws an exception if there are multiple fields with the same name
returnObject.Add(reader.GetName(i), reader[i]);
}
returnObjects.Add(returnObject);
}
}
}
return returnObjects;
}
catch (Exception ex)
{
throw;
}
}
That said, there is not much benefit from first putting the content of reader into a list of dictionaries and then into a list of Patient_Response. You might as well directly fill a List<Patient_Response> from the reader (i.e. inside the while).
A generic way to do this might be (code is not verified to compile or run):
public static List<T> GetData<T>(
SqlCommand cmd,
string connectionString,
List<SqlParameter> parameters,
Func<IDataReader, T> readerToRow)
{
List<T> returnObjects = new List<T>();
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
cmd.Connection = sqlConnection;
cmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
cmd.Parameters.Add(parameter);
}
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
returnObjects.Add(readerToRow(reader));
}
}
return returnObjects;
}
// To call above function:
List<Patient_Response> pp = Helper.GetData(
cmd,
connectionString,
parameters,
reader =>
new Patient_Response
{
FIRST_NAME = reader["FIRST_NAME"].ToString(),
LAST_NAME = reader["LAST_NAME"].ToString(),
// more columns go here
}
);

SQL Server stored procedure complains about missing parameter, but it is being set

I have this method:
public class StoredProcedureProvider : IStoredProcedureProvider
{
private readonly string _connectionString;
public StoredProcedureProvider(IDataConfig config) => _connectionString = config.ConnectionString;
public T ExecuteScalar<T>(string procedureName, IList<SqlParameter> parameters = null)
{
return Execute(procedureName, parameters, (connection, command) =>
{
connection.Open();
var result = command.ExecuteScalar();
return (T) Convert.ChangeType(result, typeof(T));
});
}
public DataSet GetDataSet(string procedureName, IList<SqlParameter> parameters = null)
{
return Execute(procedureName, parameters, (connection, command) =>
{
var dataAdapter = new SqlDataAdapter();
var dataSet = new DataSet();
dataAdapter.SelectCommand = command;
dataAdapter.Fill(dataSet);
return dataSet;
});
}
public DataRow GetDataRow(string procedureName, IList<SqlParameter> parameters = null)
{
var dataSet = GetDataSet(procedureName, parameters);
var tables = dataSet.Tables.Cast<DataTable>().ToList();
if (!tables.Any())
throw new Exception("The DataSet contains not tables");
var dataRows = dataSet.Tables[0].Rows.Cast<DataRow>().ToList();
if (!dataRows.Any())
throw new Exception("The DataTable contains no rows");
var dataRow = dataRows.First();
return dataRow;
}
private static T Execute<T>(string procedureName, IList<SqlParameter> parameters, Func<SqlConnection, SqlCommand, T> returnMethod)
{
using (var connection = new SqlConnection(_connectionString))
{
using (var command = new SqlCommand(procedureName, connection))
{
command.CommandType = CommandType.StoredProcedure;
if (parameters == null || !parameters.Any())
return returnMethod(connection, command);
foreach (var parameter in parameters)
command.Parameters.Add(parameter);
return returnMethod(connection, command);
}
}
}
}
When I invoke anything with a parameter that is not a string, I get an error:
Procedure or function 'generalLogonFromSessionID' expects parameter '#sessionID', which was not supplied.
The code I am using to test this is:
var dataRow = _storedProcedureProvider.GetDataRow("generalLogonFromSessionID", new List<SqlParameter>
{
new SqlParameter("#sessionID", SqlDbType.UniqueIdentifier, oTokenData.TokenId.Length, oTokenData.TokenId),
new SqlParameter("#ipAddress", ipAddress)
});
But if I use a stored procedure that only accepts string parameters, it works fine.
For example:
var dataRow = _storedProcedureProvider.GetDataRow("generalLogon", new List<SqlParameter>
{
new SqlParameter("#loginName", oTokenData.Update.User.EmailAddress),
new SqlParameter("#password", bookingType == BookingType.Commercial ? lastName : "password"),
new SqlParameter("#ipAddress", ipAddress)
});
Does anyone know why?
Current example is using the wrong constructor overload.
public SqlParameter(string parameterName, SqlDbType dbType, int size, string sourceColumn);
Update to use the following
new SqlParameter("#sessionID", SqlDbType.UniqueIdentifier) {
Value = oTokenData.TokenId
};
You supplied the #session parameter, but I don't see you set any value for it..
new SqlParameter("#sessionID", SqlDbType.UniqueIdentifier) { Value = oTokenData.TokenId }
Incidentally, totally appreciate that you're trying to make you life easier by creating a class here that does lots of DB donkey work and means your code becomes simpler.. but it still looks easy to make a mistake (like this). Perhaps take a look at Dapper; it would make the code to exec your proc look more like:
using (var connection = someSqlConnection)
{
var result = connection.Execute("generalLogonFromSessionID",
new {
sessionID = oTokenData.TokenId,
ipAddress = ipAddress
},
commandType: CommandType.StoredProcedure
);
//do something with result
}
No affiliation, just think it's an awesome library that is like what you're trying to achieve, but goes further.. No point reinventing the wheel?

c# genereic sql context patameter

I have a mssql-context-class for easy access to the database. It contains a function for inserting datarows, that looks like this:
public int? Insert(string tableName, Action<SqlParameterCollection> actionSqlParameterCollection)
{
using (var sqlConnection = new SqlConnection(ConnectionString))
{
sqlConnection.Open();
using (var sqlCommand = sqlConnection.CreateCommand())
{
var commandText = $"insert into {tableName} (#columns) output inserted.id values (#values)";
var valueBuilder = new StringBuilder();
var columnBuilder = new StringBuilder();
actionSqlParameterCollection?.Invoke(sqlCommand.Parameters); //Fill the parameters from outside with some values
foreach (SqlParameter parameter in sqlCommand.Parameters)
{
valueBuilder.Append($",#{parameter.ParameterName}");
columnBuilder.Append($",{parameter.ParameterName}");
}
commandText = commandText.Replace("#values", valueBuilder.ToString().Substring(1));
commandText = commandText.Replace("#columns", columnBuilder.ToString().Substring(1));
sqlCommand.CommandText = commandText;
object result = sqlCommand.ExecuteScalar();
return (int?)result;
}
}
}
Calling this would look something like this:
var context = MsSqlContext.CreateFrom("some_connectionstring");
context.Insert("myTable", parameters => {
parameters.AddWithValue("foo_1", "bar_1");
parameters.AddWithValue("foo_2", "bar_2");
});
Now i want to build a generic sql-context-class which can also handle mysql-databases. The insert-function looks like this so far:
public int? Insert(string tableName, Action<IDataParameterCollection> actionParameterCollection)
{
using (var connection = this.CreateConnection())
{
using (var command = connection.CreateCommand())
{
var commandText = $"insert into {tableName} (#field) values (#values)";
var valueBuilder = new StringBuilder();
var columnBuilder = new StringBuilder();
actionParameterCollection?.Invoke(command.Parameters);
foreach (IDbDataParameter parameter in command.Parameters)
{
valueBuilder.Append($",#{parameter.ParameterName}");
columnBuilder.Append($",{parameter.ParameterName}");
}
commandText = commandText.Replace("#values", valueBuilder.ToString().Substring(1));
commandText = commandText.Replace("#columns", columnBuilder.ToString().Substring(1));
command.CommandText = commandText;
object result = command.ExecuteScalar();
return (int?)result;
}
}
}
When i try to call the function it looks like this:
var context = SqlContext.CreateFrom(SqlProvider.MySql, "Server=localhost;Database=4713_demo;Uid=root;Pwd=;");
context.Insert("my_table", parameters =>
{
parameters.Add(?); //It expects an object
});
My Problem is, dont want to do something like
context.Insert("my_table", parameters =>
{
parameters.Add(context.CreateParameter("foo","bar"));
});
I just want to pass the parametername and the parametervalue. the context-class itself is aware of its provider and should create the parameter. How to afford that?
The solution i came up with, is this SqlParameterizer-class.
public class SqlParameterizer
{
private SqlProvider Provider { get; set; }
private List<IDbDataParameter> ParameterList { get; set; }
public SqlParameterizer(SqlProvider sqlProvider)
{
this.Provider = sqlProvider;
this.ParameterList = new List<IDbDataParameter>();
}
public void Add(string parameterName, object parameterValue)
{
switch(this.Provider)
{
case SqlProvider.MsSql:
this.ParameterList.Add(new SqlParameter(parameterName, parameterValue));
break;
case SqlProvider.MySql:
this.ParameterList.Add(new MySqlParameter(parameterName, parameterValue));
break;
case SqlProvider.OracleSql:
throw new Exception($"SqlProvider '{this.Provider}' not supported yet...");
default:
throw new Exception($"Unknown SqlProvider '{this.Provider}'");
}
}
public IDbDataParameter[] GetParameters()
{
return ParameterList.ToArray();
}
}
Using this class will look like this:
var commandText = $"insert into {tableName} (#columns) values (#values)";
var valueBuilder = new StringBuilder();
var columnBuilder = new StringBuilder();
var parameterizer = new SqlParameterizer(this.Provider);
actionValueParameterizer?.Invoke(parameterizer);
foreach(IDbDataParameter parameter in parameterizer.GetParameters())
{
command.Parameters.Add(parameter);
valueBuilder.Append($",#{parameter.ParameterName}");
columnBuilder.Append($",{parameter.ParameterName}");
}
commandText = commandText.Replace("#values", valueBuilder.ToString().Substring(1));
commandText = commandText.Replace("#columns", columnBuilder.ToString().Substring(1));
command.CommandText = commandText;
command.ExecuteNonQuery();
Calling my insert-function:
context.Insert("some_table", parameterizer =>
{
parameterizer.Add("some_column", "some_value");
});

Retrieving data from a database through SqlDataReader and converting it to JSON

I want to convert my retrieved data into JSON; I am using a SqlDataReader for retrieving. For that I need to store my data into var.
I am getting this error:
An exception of type 'System.Reflection.AmbiguousMatchException' occurred in mscorlib.dll but was not handled in user code
Additional information: Ambiguous match found.
in the following code:
public string GetDetails(int Id)
{
var jsonDoc = "";
string con = "server=FACULTY01\\SQLSERVER2012ENT; database=SampleDb; uid=sa; pwd=sa9";
SqlConnection scon = new SqlConnection(con);
string qry = "Select * from Information where ID =" + Id;
SqlCommand cmd = new SqlCommand(qry, scon);
scon.Open();
SqlDataReader rdr = cmd.ExecuteReader();
var details = rdr;
JavaScriptSerializer jss = new JavaScriptSerializer();
jsonDoc = jss.Serialize(details);
scon.Close();
return jsonDoc;
}
You can't directly serialize a SqlDataReader and expect the output to contain the data from the SQL query. That's not the way that SqlDataReader works. SqlDataReader is a means to retrieve data from the SQL result. You can use it to populate some other object (such as a Dictionary<string, object> or a strongly-typed class you define) which you can then hand to the serializer to produce JSON.
Try the code below instead. (Also note the use of using statements and parameterized SQL in keeping with good coding practices, as mentioned by #Jon Skeet.)
public static string GetDetails(int Id)
{
string con = "server=FACULTY01\\SQLSERVER2012ENT; database=SampleDb; uid=sa; pwd=sa9";
using (SqlConnection scon = new SqlConnection(con))
{
string qry = "Select * from Information where ID = #id";
SqlCommand cmd = new SqlCommand(qry, scon);
cmd.Parameters.AddWithValue("#id", Id);
scon.Open();
var details = new Dictionary<string, object>();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
if (rdr.HasRows && rdr.Read())
{
for (int i = 0; i < rdr.FieldCount; i++)
{
details.Add(rdr.GetName(i), rdr.IsDBNull(i) ? null : rdr.GetValue(i));
}
}
}
JavaScriptSerializer jss = new JavaScriptSerializer();
string jsonDoc = jss.Serialize(details);
scon.Close();
return jsonDoc;
}
}
Note that the above code is expecting to get a single row back from the reader. If you are expecting more than one row then you will need to use another loop and put the resulting data into a List<Dictionary<string, object>> instead. Here is the part you would need to change:
...
var details = new List<Dictionary<string, object>>();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
if (rdr.HasRows)
{
while (rdr.Read())
{
var dict = new Dictionary<string, object>();
for (int i = 0; i < rdr.FieldCount; i++)
{
dict.Add(rdr.GetName(i), rdr.IsDBNull(i) ? null : rdr.GetValue(i));
}
details.Add(dict);
}
}
}
...
You need a loop to store the data from the reader. Use a DataTable to store the full table. A single variable will store single column data.
while (rdr.Read())
{
var details = rdr["columnName"];
}
Encountered this post as it comes first in google search.
Have given an answer here (based on this):
The requirement is somewhat similar that is of converting sqldatareader result to a json string.
public static class MyExtensions
{
public async static Task<string> toJSON(this SqlDataReader reader)
{
var results = await reader.GetSerialized();
return JsonConvert.SerializeObject(results, Formatting.Indented);
}
public async static Task<IEnumerable<Dictionary<string, object>>> GetSerialized(this SqlDataReader reader)
{
var results = new List<Dictionary<string, object>>();
var cols = new List<string>();
for (var i = 0; i < reader.FieldCount; i++)
cols.Add(reader.GetName(i));
while (await reader.ReadAsync())
results.Add(SerializeRow(cols, reader));
return results;
}
private static Dictionary<string, object> SerializeRow(IEnumerable<string> cols,
SqlDataReader reader)
{
var result = new Dictionary<string, object>();
foreach (var col in cols)
result.Add(col, reader[col]);
return result;
}
}
Use As :
var result = await reader.GetSerialized(); //to get the result object
or
string strResult = await reader.toJSON(); //to get the result string

Can I create an anonymous type collection from a DataReader?

I have this code:
var query = "SELECT * FROM Cats";
var conn = new SqlConnection(sqlConnectionString);
conn.Open();
var cmd = new SqlCommand(query);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
var CatName = reader.GetString(0);
var CatDOB = reader.GetDateTime(1);
var CatStatus = reader.GetInt32(2);
}
I'd like to pull the rows out into an anonymous type collection, which I'd normally do using LINQ to iterate, but I an not sure if it's possible due to the way you have to call .Read() each time to get the next row.
Is there a way to do this?
You can create helper generic method and let compiler infer type parameter:
private IEnumerable<T> Select<T>(DbDataReader reader, Func<DbDataReader, T> selector)
{
while(reader.Read())
{
yield return selector(reader);
}
}
usage:
var items = SelectFromReader(reader, r => new { CatName = r.GetString(0), CarDOB = r.GetDateTime(1), CatStatus = r.GetInt32(2) });
You can even make the method an extension method on DbDataReader:
public static IEnumerable<T> Select<T>(this DbDataReader reader, Func<DbDataReader, T> selector)
{
while (reader.Read())
{
yield return selector(reader);
}
}
and use it like that:
var items = reader.Select(r => new { CatName = r.GetString(0), CarDOB = r.GetDateTime(1), CatStatus = r.GetInt32(2) });
Here is an example of doing it with dynamic (which I think is easier to work with) but some may feel does not adhere to the letter of your question.
Call it like this:
var result = SelectIntoList("SELECT * FROM Cats",sqlconnectionString);
You could (like I did) put it into a static class in a separate file for easier maintanence.
public static IEnumerable<dynamic> SelectIntoList(string SQLselect, string connectionString, CommandType cType = CommandType.Text)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandType = cType;
cmd.CommandText = SQLselect;
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read()) // read the first one to get the columns collection
{
var cols = reader.GetSchemaTable()
.Rows
.OfType<DataRow>()
.Select(r => r["ColumnName"]);
do
{
dynamic t = new System.Dynamic.ExpandoObject();
foreach (string col in cols)
{
((IDictionary<System.String, System.Object>)t)[col] = reader[col];
}
yield return t;
} while (reader.Read());
}
}
conn.Close();
}
}
}
It's possible, although not particularly neat. We'll need to create a new method that will allow us to create an empty sequence that allows for type inference off of a dummy value for starters:
public static IEnumerable<T> Empty<T>(T dummyValue)
{
return Enumerable.Empty<T>();
}
This lets us create a list of an anonymous type:
var list = Empty(new
{
CatName = "",
CatDOB = DateTime.Today,
CatStatus = 0
}).ToList();
(The item here isn't used.)
Now we can add our anonymous types to this list:
var cmd = new SqlCommand(query);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
list.Add(new
{
CatName = reader.GetString(0),
CatDOB = reader.GetDateTime(1),
CatStatus = reader.GetInt32(2),
});
}
Of course, using a named type would likely be easier, so I would suggest using one unless there is a real compelling reason not to do so. That is especially true if you plan to use the list outside of the scope it's created in.
Technically, it may not answer your question, but simply don't use a reader. Instead use a SqlDataAdapter to Fill a DataSet, if you can. Take the 0th Table of that DataSet, and select a new anonymous object from the Rows collection.
using System.Data; // and project must reference System.Data.DataSetExtensions
var ds = new DataSet();
using (var conn = DbContext.Database.GetDbConnection())
using (var cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlText;
conn.Open();
(new SqlDataAdapter(cmd)).Fill(ds);
}
var rows = ds.Tables[0].AsEnumerable(); // AsEnumerable() is the extension
var anons = rows
.Select(r => new { Val = r["Val"] })
.ToList();

Categories