SqlServerHelper.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Configuration;
  3. using System.Data.SqlClient;
  4. using System.Data;
  5. using Common;
  6. namespace LisPacsDataUpload
  7. {
  8. public class SqlServerHelper
  9. {
  10. //
  11. public static string GetConnStr()
  12. {
  13. return ConfigurationManager.ConnectionStrings["SqlServerConnection"].ConnectionString;
  14. }
  15. public static DataTable ExecuteQuery(string query)
  16. {
  17. using (SqlConnection connection = new SqlConnection(GetConnStr()))
  18. {
  19. SqlCommand command = new SqlCommand(query, connection);
  20. try
  21. {
  22. connection.Open();
  23. SqlDataAdapter adapter = new SqlDataAdapter(command);
  24. DataTable dataTable = new DataTable();
  25. adapter.Fill(dataTable);
  26. return dataTable;
  27. }
  28. catch (Exception ex)
  29. {
  30. Console.WriteLine("查询数据时出现错误:" + ex.Message);
  31. return null;
  32. }
  33. }
  34. }
  35. public static DataRow ExecuteSingleRowQuery(string query)
  36. {
  37. try
  38. {
  39. using (SqlConnection connection = new SqlConnection(GetConnStr()))
  40. {
  41. SqlCommand command = new SqlCommand(query, connection);
  42. connection.Open();
  43. SqlDataAdapter adapter = new SqlDataAdapter(command);
  44. DataTable dataTable = new DataTable();
  45. adapter.Fill(dataTable);
  46. LogHelper.Info("dataTable.Rows.Count"+ dataTable.Rows.Count);
  47. if (dataTable.Rows.Count > 0)
  48. {
  49. return dataTable.Rows[0];
  50. }
  51. else
  52. {
  53. return null;
  54. }
  55. }
  56. }
  57. catch (Exception ex)
  58. {
  59. LogHelper.Info("查询数据时出现错误:" + ex.Message);
  60. return null;
  61. }
  62. }
  63. }
  64. }