SqlCommandUtility.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SinoChemCommonUtilities
  9. {
  10. public delegate T ConstructThing<T>(SqlDataReader rdr);
  11. public class SqlCommandUtility
  12. {
  13. private SqlConnection conn;
  14. private string _connectionString;
  15. public SqlCommandUtility(string connString)
  16. {
  17. conn = new SqlConnection(connString);
  18. _connectionString = connString;
  19. }
  20. public T ReadData<T>(string querySql, ConstructThing<T> constructThing)
  21. {
  22. T thing = default(T);
  23. try
  24. {
  25. using (var connection = new SqlConnection(_connectionString))
  26. {
  27. connection.Open();
  28. using (var command = new SqlCommand(querySql, connection))
  29. using (var sqlDataReader = command.ExecuteReader())
  30. {
  31. thing = constructThing(sqlDataReader);
  32. return thing;
  33. }
  34. }
  35. }
  36. catch (Exception ex)
  37. {
  38. Console.WriteLine(ex.ToString());
  39. return thing;
  40. }
  41. /*
  42. try
  43. {
  44. // Open the connection
  45. conn.Open();
  46. // 1. Instantiate a new command with a query and connection
  47. SqlCommand cmd = new SqlCommand(querySql, conn);
  48. // 2. Call Execute reader to get query results
  49. reader = cmd.ExecuteReader();
  50. // 3.
  51. thing = constructThing(reader);
  52. return thing;
  53. }
  54. finally
  55. {
  56. if (reader != null)
  57. {
  58. reader.Close();
  59. }
  60. // Close the connection
  61. if (conn != null)
  62. {
  63. conn.Close();
  64. }
  65. }
  66. */
  67. }
  68. public object Insertdata(string insertSql, bool toExeScalar)
  69. {
  70. object scalarObj = null;
  71. try
  72. {
  73. // Open the connection
  74. conn.Open();
  75. // 1. Instantiate a new command with a query and connection
  76. SqlCommand cmd = new SqlCommand(insertSql, conn);
  77. // 2. Call to send command
  78. if (toExeScalar)
  79. {
  80. scalarObj = cmd.ExecuteScalar();
  81. }
  82. else
  83. {
  84. cmd.ExecuteNonQuery();
  85. }
  86. return scalarObj;
  87. }
  88. finally
  89. {
  90. // Close the connection
  91. if (conn != null)
  92. {
  93. conn.Close();
  94. }
  95. }
  96. }
  97. public void UpdateData(string updateSql)
  98. {
  99. try
  100. {
  101. // Open the connection
  102. conn.Open();
  103. // 1. Instantiate a new command with command text only
  104. SqlCommand cmd = new SqlCommand(updateSql);
  105. // 2. Set the Connection property
  106. cmd.Connection = conn;
  107. // 3. Call ExecuteNonQuery to send command
  108. cmd.ExecuteNonQuery();
  109. }
  110. finally
  111. {
  112. // Close the connection
  113. if (conn != null)
  114. {
  115. conn.Close();
  116. }
  117. }
  118. }
  119. public void DeleteData(string deleteSql)
  120. {
  121. try
  122. {
  123. // Open the connection
  124. conn.Open();
  125. // 1. Instantiate a new command
  126. SqlCommand cmd = new SqlCommand();
  127. // 2. Set the CommandText property
  128. cmd.CommandText = deleteSql;
  129. // 3. Set the Connection property
  130. cmd.Connection = conn;
  131. // 4. Call ExecuteNonQuery to send command
  132. cmd.ExecuteNonQuery();
  133. }
  134. finally
  135. {
  136. // Close the connection
  137. if (conn != null)
  138. {
  139. conn.Close();
  140. }
  141. }
  142. }
  143. }
  144. }