热门IT资讯网

WPF中SqlHelper

发表于:2024-11-26 作者:热门IT资讯网编辑
编辑最后更新 2024年11月26日,using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Confi
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. namespace 数据绑定
  9. {
  10. class SqlHelper
  11. {
  12. private static string strconn= ConfigurationManager.ConnectionStrings["dbstrConn"].ConnectionString;
  13. public static int ExecuteNonQuery(string sql)
  14. {
  15. using (SqlConnection conn = new SqlConnection(strconn))
  16. {
  17. conn.Open();
  18. using (SqlCommand cmd = conn.CreateCommand())
  19. {
  20. cmd.CommandText = sql;
  21. return cmd.ExecuteNonQuery();
  22. }
  23. }
  24. }
  25. public static object ExecuteScalar(string sql)
  26. {
  27. using (SqlConnection conn = new SqlConnection(strconn))
  28. {
  29. conn.Open();
  30. using (SqlCommand cmd = conn.CreateCommand())
  31. {
  32. cmd.CommandText = sql;
  33. return cmd.ExecuteScalar();
  34. }
  35. }
  36. }
  37. //查询少量数据
  38. public static DataTable ExecuteTable(string sql)
  39. {
  40. using (SqlConnection conn = new SqlConnection(strconn))
  41. {
  42. conn.Open();
  43. using (SqlCommand cmd = conn.CreateCommand())
  44. {
  45. cmd.CommandText = sql;
  46. SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  47. DataSet dataset = new DataSet();
  48. adapter.Fill(dataset);
  49. return dataset.Tables[0];
  50. }
  51. }
  52. }
  53. }
  54. }
0