Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DBoperations
- {
- private string querystringval;
- private string logfileloc;
- private string connectionstringval;
- LogToFile logobj = new LogToFile();
- SqlConnection SqlConn = new SqlConnection();
- public string logfilelocval
- {
- get { return logfileloc; }
- set { logfileloc = value; }
- }
- public string queryValue
- {
- get { return querystringval; }
- set { querystringval = value; }
- }
- public string connectionvalue
- {
- get { return connectionstringval; }
- set { connectionstringval = value; }
- }
- public Boolean connecttodb()
- {
- logobj.fileName = logfilelocval;
- //SqlConnection SqlConn = new SqlConnection(connectionvalue);
- SqlConn.ConnectionString = connectionvalue;
- try
- {
- SqlConn.Open();
- logobj.MyLogFile("**SUCCESS** ", "Database connection opened");
- return true;
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- logobj.MyLogFile("**FAILURE**", "Database Connection Failed" + e.Message);
- return false;
- throw new IndexOutOfRangeException();
- }
- }
- public string executeaquery()
- {
- try
- {
- SqlCommand querystring = new SqlCommand(queryValue, SqlConn);
- querystring.CommandTimeout = 90;
- querystring.ExecuteNonQuery();
- logobj.MyLogFile("**SUCCESS** ", "Query Executed Successfully.");
- querystring.Dispose();
- return "True";
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- logobj.MyLogFile("**FAILURE**", "Query did not execute." + e.Message);
- return e.Message;
- throw new IndexOutOfRangeException();
- }
- }
- }}
- using (SqlConnection c = new SqlConnection(connString))
- {
- var customer = c.Query<Customer>(
- "SELECT FirstName, LastName, DateOfBirth FROM Customer WHERE CustomerID = @CustomerID",
- new { CustomerID = 1 });
- }
- public interface IMyConnection
- {
- TModel Query<TModel>(string sql, object parms);
- int Execute(string sql, object parms);
- }
- public class MsSqlMyConnection : IMyConnection
- {
- public TModel Query<TModel>(string sql, object parms)
- {
- using (SqlConnection c = new SqlConnection(connString))
- {
- return c.Query<TModel>(sql, parms);
- }
- }
- public int Execute(string sql, object parms)
- {
- using (SqlConnection c = new SqlConnection(connString))
- {
- return c.Execute(sql, parms);
- }
- }
- }
- var c = new MsSqlMyConnection();
- var customer = c.Query<Customer>(
- "SELECT FirstName, LastName, DateOfBirth FROM Customer WHERE CustomerID = @CustomerID",
- new { CustomerID = 1 });
- var c = new MsSqlMyConnection();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement