Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. public class DBoperations
  2. {
  3. private string querystringval;
  4. private string logfileloc;
  5. private string connectionstringval;
  6. LogToFile logobj = new LogToFile();
  7. SqlConnection SqlConn = new SqlConnection();
  8. public string logfilelocval
  9. {
  10. get { return logfileloc; }
  11. set { logfileloc = value; }
  12. }
  13. public string queryValue
  14. {
  15. get { return querystringval; }
  16. set { querystringval = value; }
  17. }
  18. public string connectionvalue
  19. {
  20. get { return connectionstringval; }
  21. set { connectionstringval = value; }
  22. }
  23.  
  24. public Boolean connecttodb()
  25. {
  26. logobj.fileName = logfilelocval;
  27. //SqlConnection SqlConn = new SqlConnection(connectionvalue);
  28. SqlConn.ConnectionString = connectionvalue;
  29. try
  30. {
  31. SqlConn.Open();
  32. logobj.MyLogFile("**SUCCESS** ", "Database connection opened");
  33. return true;
  34. }
  35. catch (Exception e)
  36. {
  37. Console.WriteLine(e.ToString());
  38. logobj.MyLogFile("**FAILURE**", "Database Connection Failed" + e.Message);
  39. return false;
  40. throw new IndexOutOfRangeException();
  41. }
  42. }
  43. public string executeaquery()
  44. {
  45. try
  46. {
  47. SqlCommand querystring = new SqlCommand(queryValue, SqlConn);
  48. querystring.CommandTimeout = 90;
  49. querystring.ExecuteNonQuery();
  50. logobj.MyLogFile("**SUCCESS** ", "Query Executed Successfully.");
  51. querystring.Dispose();
  52. return "True";
  53. }
  54. catch (Exception e)
  55. {
  56. Console.WriteLine(e.ToString());
  57. logobj.MyLogFile("**FAILURE**", "Query did not execute." + e.Message);
  58. return e.Message;
  59. throw new IndexOutOfRangeException();
  60. }
  61. }
  62. }}
  63.  
  64. using (SqlConnection c = new SqlConnection(connString))
  65. {
  66. var customer = c.Query<Customer>(
  67. "SELECT FirstName, LastName, DateOfBirth FROM Customer WHERE CustomerID = @CustomerID",
  68. new { CustomerID = 1 });
  69. }
  70.  
  71. public interface IMyConnection
  72. {
  73. TModel Query<TModel>(string sql, object parms);
  74.  
  75. int Execute(string sql, object parms);
  76. }
  77.  
  78. public class MsSqlMyConnection : IMyConnection
  79. {
  80. public TModel Query<TModel>(string sql, object parms)
  81. {
  82. using (SqlConnection c = new SqlConnection(connString))
  83. {
  84. return c.Query<TModel>(sql, parms);
  85. }
  86. }
  87.  
  88. public int Execute(string sql, object parms)
  89. {
  90. using (SqlConnection c = new SqlConnection(connString))
  91. {
  92. return c.Execute(sql, parms);
  93. }
  94. }
  95. }
  96.  
  97. var c = new MsSqlMyConnection();
  98. var customer = c.Query<Customer>(
  99. "SELECT FirstName, LastName, DateOfBirth FROM Customer WHERE CustomerID = @CustomerID",
  100. new { CustomerID = 1 });
  101.  
  102. var c = new MsSqlMyConnection();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement