SQL.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Data.SqlClient;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Windows;
  6. namespace Курсовой_проект_3._1
  7. {
  8. class SQL
  9. {
  10. string connectionString;
  11. public string connectionName { get; set; }
  12. public SQL(string connectionName)
  13. {
  14. this.connectionName = connectionName;
  15. connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
  16. }
  17. public DataTable SendSelectQuery(string sqlQuery)
  18. {
  19. DataTable dataTable = new DataTable();
  20. SqlConnection connection = null;
  21. try
  22. {
  23. connection = new SqlConnection(connectionString);
  24. connection.Open();
  25. SqlCommand command = new SqlCommand(sqlQuery, connection);
  26. SqlDataAdapter adapter = new SqlDataAdapter(command);
  27. adapter.Fill(dataTable);
  28. }
  29. catch (Exception ex)
  30. {
  31. MessageBox.Show(ex.Message);
  32. return null;
  33. }
  34. finally
  35. {
  36. if (connection != null)
  37. {
  38. connection.Close();
  39. }
  40. }
  41. return dataTable;
  42. }
  43. }
  44. }