SqlServerHelper.cs 2.2 KB

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