马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

精简Command版SqlHelper

[复制链接]
查看1273 | 回复1 | 2019-12-26 09:07:13 | 显示全部楼层 |阅读模式

我在写CSharp步伐对数据库举利用用时发现Connection对象起到了连接数据库的做用,实际实行SQL语句使用的是Command对象的方法,以是对SqlHelper举行了重写,详细如下:

一、创建一个ParameterCommand对象,只包罗CommandText和Parameters属性,紧张用于以事件的方式批量实行SQL语句,我感觉比创建List commandTexts和List> paras两个参数方便,也不轻易堕落

  1. public class ParameterCommand
  2. {
  3. private List<DbParameter> paras = new List<DbParameter>();
  4. public string CommandText { get; set; }
  5. public List<DbParameter> Parameters
  6. {
  7. get
  8. {
  9. return paras;
  10. }
  11. }
  12. }
复制代码

二、精简Command版SqlHelper代码如下,传入Command对象做为参数用于实行SQL语句

  1. public static class SqlHelper
  2. {
  3. private static void ResetCommandProperty(DbCommand command, string commandText, params DbParameter[] paras)
  4. {
  5. command.Parameters.Clear();
  6. command.CommandText = commandText;
  7. command.Parameters.AddRange(paras);
  8. }
  9. public static void ExecuteNonQuery(DbCommand command, List<ParameterCommand> paraCommands)
  10. {
  11. command.Transaction = command.Connection.BeginTransaction();
  12. foreach (ParameterCommand paraCommand in paraCommands)
  13. {
  14. try
  15. {
  16. ResetCommandProperty(command, paraCommand.CommandText,paraCommand.Parameters.ToArray());
  17. command.ExecuteNonQuery();
  18. }
  19. catch (Exception ex)
  20. {
  21. command.Transaction.Rollback();
  22. throw ex;
  23. }
  24. }
  25. command.Transaction.Commit();
  26. }
  27. public static void ExecuteNonQuery(DbCommand command, string commandText, params DbParameter[] paras)
  28. {
  29. ResetCommandProperty(command, commandText, paras);
  30. command.ExecuteNonQuery();
  31. }
  32. public static DbDataReader ExecuteReader(DbCommand command, ParameterCommand paraCommand)
  33. {
  34. ResetCommandProperty(command, paraCommand.CommandText, paraCommand.Parameters.ToArray());
  35. return command.ExecuteReader();
  36. }
  37. public static DbDataReader ExecuteReader(DbCommand command, string commandText, params DbParameter[] paras)
  38. {
  39. ResetCommandProperty(command, commandText, paras);
  40. return command.ExecuteReader();
  41. }
  42. public static object ExecuteScalar(DbCommand command, ParameterCommand paraCommand)
  43. {
  44. ResetCommandProperty(command, paraCommand.CommandText,paraCommand.Parameters.ToArray());
  45. return command.ExecuteScalar();
  46. }
  47. public static object ExecuteScalar(DbCommand command, string commandText, params DbParameter[] paras)
  48. {
  49. ResetCommandProperty(command, commandText, paras);
  50. return command.ExecuteScalar();
  51. }
  52. public static DataTable ExecuteTable(DbCommand command, ParameterCommand paraCommand)
  53. {
  54. return ExecuteTable(command, paraCommand.CommandText, paraCommand.Parameters.ToArray());
  55. }
  56. public static DataTable ExecuteTable(DbCommand command, string commandText, params DbParameter[] paras)
  57. {
  58. DataTable table = new DataTable();
  59. ResetCommandProperty(command, commandText, paras);
  60. using (DbDataAdapter adapter = DbProviderFactories.GetFactory(command.Connection).CreateDataAdapter())
  61. {
  62. adapter.SelectCommand = command;
  63. adapter.Fill(table);
  64. }
  65. return table;
  66. }
  67. }
复制代码

三、封装的通用DatabaseClient

  1. public abstract class DatabaseClient
  2. {
  3. private DbConnection connection;
  4. public abstract DbConnection GetConnection();
  5. private DbCommand GetCommand()
  6. {
  7. if (connection == null)
  8. {
  9. connection = GetConnection();
  10. }
  11. if (connection.State == ConnectionState.Broken)
  12. {
  13. connection.Close();
  14. }
  15. if (connection.State == ConnectionState.Closed)
  16. {
  17. connection.Open();
  18. }
  19. return connection.CreateCommand();
  20. }
  21. public void ExecuteNonQuery(List<ParameterCommand> paraCommands)
  22. {
  23. using (DbCommand command = GetCommand())
  24. {
  25. SqlHelper.ExecuteNonQuery(command, paraCommands);
  26. }
  27. }
  28. public void ExecuteNonQuery(string commandText, params DbParameter[] paras)
  29. {
  30. using (DbCommand command = GetCommand())
  31. {
  32. SqlHelper.ExecuteNonQuery(command, commandText, paras);
  33. }
  34. }
  35. public DbDataReader ExecuteReader(ParameterCommand paraCommand)
  36. {
  37. using (DbCommand command = GetCommand())
  38. {
  39. return SqlHelper.ExecuteReader(command, paraCommand);
  40. }
  41. }
  42. public DbDataReader ExecuteReader(string commandText, params DbParameter[] paras)
  43. {
  44. using (DbCommand command = GetCommand())
  45. {
  46. return SqlHelper.ExecuteReader(command, commandText, paras);
  47. }
  48. }
  49. public object ExecuteScalar(ParameterCommand paraCommand)
  50. {
  51. using (DbCommand command = GetCommand())
  52. {
  53. return SqlHelper.ExecuteScalar(command, paraCommand);
  54. }
  55. }
  56. public object ExecuteScalar(string commandText, params DbParameter[] paras)
  57. {
  58. using (DbCommand command = GetCommand())
  59. {
  60. return SqlHelper.ExecuteScalar(command, commandText, paras);
  61. }
  62. }
  63. public DataTable ExecuteTable(ParameterCommand paraCommand)
  64. {
  65. using (DbCommand command = GetCommand())
  66. {
  67. return SqlHelper.ExecuteTable(command, paraCommand);
  68. }
  69. }
  70. public DataTable ExecuteTable(string commandText, params DbParameter[] paras)
  71. {
  72. using (DbCommand command = GetCommand())
  73. {
  74. return SqlHelper.ExecuteTable(command, commandText, paras);
  75. }
  76. }
  77. }
复制代码

四、举个栗子:MySQL版客户端

  1. public class MySqlClient : DatabaseClient
  2. {
  3. private string connectionString;
  4. public MySqlClient(string dataSource, string userName, string password)
  5. {
  6. connectionString = "DataSource=" + dataSource + ";UserName=" + userName + ";Password=" + password + ";Charset=utf8";
  7. }
  8. public MySqlClient(string dataSource, string userName, string password, string database)
  9. {
  10. connectionString = "DataSource=" + dataSource + ";UserName=" + userName + ";Password=" + password + "Database=" + database + ";Charset=utf8";
  11. }
  12. public override System.Data.Common.DbConnection GetConnection()
  13. {
  14. return new MySqlConnection(connectionString);
  15. }
  16. }
复制代码
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则