For query my sql database with ado.net we always write open and close connections code in every method. I have make some generic functions here that can reuse the code. You no need to write open and close connections code in every method.
//This method is for execute the insert sql. This returns the new added record's primary key column.
public static long ExecuteInsertSql(string sql, Dictionary<string, object> parameters)
{
using (MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString))
{
if (connection.State == ConnectionState.Closed)
connection.Open();
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
ProcessCommandParameters(parameters, command);
command.ExecuteNonQuery();
return command.LastInsertedId;
}
}
}
//Following method is for execute the sql queries that doen't return anything.
public static void ExecuteSqlQuery(string sql, Dictionary<string, object> parameters)
{
using (MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString))
{
if (connection.State == ConnectionState.Closed)
connection.Open();
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
ProcessCommandParameters(parameters, command);
command.ExecuteNonQuery();
}
}
}
//Following method returns the single record's details in dictionary object.
public static Dictionary<string, object> ExecuteSingleSelectSqlQuery(string sql, Dictionary<string, object> parameters = null)
{
Dictionary<string, object> retreivedData = null;
using (MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString))
{
if (connection.State == ConnectionState.Closed)
connection.Open();
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
ProcessCommandParameters(parameters, command);
var reader = command.ExecuteReader();
if (reader.HasRows)
{
retreivedData = new Dictionary<string, object>();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
retreivedData[reader.GetName(i)] = reader[i];
}
}
}
return retreivedData;
}
}
}
//Following method is for get the collection of records in dictionary object.
public static Dictionary<int, Dictionary<string, object>> ExecuteSelectAllSqlQuery(string sql, Dictionary<string, object> parameters)
{
Dictionary<int, Dictionary<string, object>> retreivedData = null;
using (MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString))
{
if (connection.State == ConnectionState.Closed)
connection.Open();
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
ProcessCommandParameters(parameters, command);
var reader = command.ExecuteReader();
if (reader.HasRows)
{
retreivedData = new Dictionary<int, Dictionary<string, object>>();
int count = 0;
Dictionary<string, object> readRow = null;
while (reader.Read())
{
readRow = new Dictionary<string, object>();
for (int i = 0; i < reader.FieldCount; i++)
{
readRow[reader.GetName(i)] = reader[i];
}
retreivedData.Add(count, readRow);
count++;
}
}
return retreivedData;
}
}
}
//This is a common method to add the paramters in the command. This is being used in all above methods.
private static void ProcessCommandParameters(Dictionary<string, object> parameters, MySqlCommand command)
{
if (parameters != null)
{
foreach (var parameter in parameters)
{
if (!parameter.Key.StartsWith("@"))
{
command.Parameters.Add(new MySqlParameter { ParameterName = "@" + parameter.Key, Value = parameter.Value });
}
else
{
command.Parameters.Add(new MySqlParameter { ParameterName = parameter.Key, Value = parameter.Value });
}
}
}
}
//This method is for execute the insert sql. This returns the new added record's primary key column.
public static long ExecuteInsertSql(string sql, Dictionary<string, object> parameters)
{
using (MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString))
{
if (connection.State == ConnectionState.Closed)
connection.Open();
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
ProcessCommandParameters(parameters, command);
command.ExecuteNonQuery();
return command.LastInsertedId;
}
}
}
//Following method is for execute the sql queries that doen't return anything.
public static void ExecuteSqlQuery(string sql, Dictionary<string, object> parameters)
{
using (MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString))
{
if (connection.State == ConnectionState.Closed)
connection.Open();
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
ProcessCommandParameters(parameters, command);
command.ExecuteNonQuery();
}
}
}
//Following method returns the single record's details in dictionary object.
public static Dictionary<string, object> ExecuteSingleSelectSqlQuery(string sql, Dictionary<string, object> parameters = null)
{
Dictionary<string, object> retreivedData = null;
using (MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString))
{
if (connection.State == ConnectionState.Closed)
connection.Open();
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
ProcessCommandParameters(parameters, command);
var reader = command.ExecuteReader();
if (reader.HasRows)
{
retreivedData = new Dictionary<string, object>();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
retreivedData[reader.GetName(i)] = reader[i];
}
}
}
return retreivedData;
}
}
}
//Following method is for get the collection of records in dictionary object.
public static Dictionary<int, Dictionary<string, object>> ExecuteSelectAllSqlQuery(string sql, Dictionary<string, object> parameters)
{
Dictionary<int, Dictionary<string, object>> retreivedData = null;
using (MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString))
{
if (connection.State == ConnectionState.Closed)
connection.Open();
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
ProcessCommandParameters(parameters, command);
var reader = command.ExecuteReader();
if (reader.HasRows)
{
retreivedData = new Dictionary<int, Dictionary<string, object>>();
int count = 0;
Dictionary<string, object> readRow = null;
while (reader.Read())
{
readRow = new Dictionary<string, object>();
for (int i = 0; i < reader.FieldCount; i++)
{
readRow[reader.GetName(i)] = reader[i];
}
retreivedData.Add(count, readRow);
count++;
}
}
return retreivedData;
}
}
}
//This is a common method to add the paramters in the command. This is being used in all above methods.
private static void ProcessCommandParameters(Dictionary<string, object> parameters, MySqlCommand command)
{
if (parameters != null)
{
foreach (var parameter in parameters)
{
if (!parameter.Key.StartsWith("@"))
{
command.Parameters.Add(new MySqlParameter { ParameterName = "@" + parameter.Key, Value = parameter.Value });
}
else
{
command.Parameters.Add(new MySqlParameter { ParameterName = parameter.Key, Value = parameter.Value });
}
}
}
}
No comments:
Post a Comment