首页 > 数据库 >MySql基于ADO.NET方式访问数据库ADOHelper封装MySqlHelper

MySql基于ADO.NET方式访问数据库ADOHelper封装MySqlHelper

时间:2022-11-18 13:04:13浏览次数:38  
标签:ADOHelper object new IDbCommand command MySql override ADO public


/// <summary>
    /// The MySql class is intended to encapsulate high performance, scalable best practices for 
    /// common uses of the MySqlClient ADO.NET provider.  It is created using the abstract factory in AdoHelper.
    /// </summary>
    public class MySqlDb : AdoHelper
    {
        /// <summary>
        /// Create a MySQL Helper.  Needs to be a default constructor so that the Factory can create it
        /// </summary>
        public MySqlDb()
        {
        }


        #region Overrides
        /// <summary>
        /// Returns an array of SqlParameters of the specified size
        /// </summary>
        /// <param name="size">size of the array</param>
        /// <returns>The array of SqlParameters</returns>
        protected override IDataParameter[] GetDataParameters(int size)
        {
            return new MySqlParameter[size];
        }


        /// <summary>
        /// Returns a SqlConnection object for the given connection string
        /// </summary>
        /// <param name="connectionString">The connection string to be used to create the connection</param>
        /// <returns>A SqlConnection object</returns>
        public override IDbConnection GetConnection(string connectionString)
        {
            return new MySqlConnection(connectionString);
        }


        /// <summary>
        /// Returns a SqlDataAdapter object
        /// </summary>
        /// <returns>The SqlDataAdapter</returns>
        public override IDbDataAdapter GetDataAdapter()
        {
            return new MySqlDataAdapter();
        }


        /// <summary>
        /// Calls the CommandBuilder.DeriveParameters method for the specified provider, doing any setup and cleanup necessary
        /// </summary>
        /// <param name="cmd">The IDbCommand referencing the stored procedure from which the parameter information is to be derived. The derived parameters are added to the Parameters collection of the IDbCommand. </param>
        public override void DeriveParameters(IDbCommand cmd)
        {
            bool mustCloseConnection = false;


            if (!(cmd is MySqlCommand))
                throw new ArgumentException("The command provided is not a SqlCommand instance.", "cmd");


            if (cmd.Connection.State != ConnectionState.Open)
            {
                cmd.Connection.Open();
                mustCloseConnection = true;
            }


            MySqlCommandBuilder.DeriveParameters((MySqlCommand)cmd);


            if (mustCloseConnection)
            {
                cmd.Connection.Close();
            }
        }


        /// <summary>
        /// Returns a SqlParameter object
        /// </summary>
        /// <returns>The SqlParameter object</returns>
        public override IDataParameter GetParameter()
        {
            return new MySqlParameter();
        }


        /// <summary>
        /// Detach the IDataParameters from the command object, so they can be used again.
        /// </summary>
        /// <param name="command">command object to clear</param>
        protected override void ClearCommand(IDbCommand command)
        {
            // HACK: There is a problem here, the output parameter values are fletched 
            // when the reader is closed, so if the parameters are detached from the command
            // then the IDataReader can磘 set its values. 
            // When this happen, the parameters can磘 be used again in other command.
            bool canClear = true;


            foreach (IDataParameter commandParameter in command.Parameters)
            {
                if (commandParameter.Direction != ParameterDirection.Input)
                    canClear = false;


            }
            if (canClear)
            {
                command.Parameters.Clear();
            }
        }


        /// <summary>
        /// This cleans up the parameter syntax for an SQL Server call.  This was split out from PrepareCommand so that it could be called independently.
        /// </summary>
        /// <param name="command">An IDbCommand object containing the CommandText to clean.</param>
        public override void CleanParameterSyntax(IDbCommand command)
        {
            // do nothing for SQL
        }


        /// <summary>
        /// Execute a SqlCommand (that returns a resultset) against the provided SqlConnection. 
        /// </summary>
        /// <example>
        /// <code>
        /// XmlReader r = helper.ExecuteXmlReader(command);
        /// </code></example>
        /// <param name="command">The IDbCommand to execute</param>
        /// <returns>An XmlReader containing the resultset generated by the command</returns>
        public override XmlReader ExecuteXmlReader(IDbCommand command)
        {
            bool mustCloseConnection = false;


            if (command.Connection.State != ConnectionState.Open)
            {
                command.Connection.Open();
                mustCloseConnection = true;
            }


            CleanParameterSyntax(command);
            MySqlDataAdapter da = new MySqlDataAdapter((MySqlCommand)command);
            DataSet ds = new DataSet();


            da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            da.Fill(ds);


            StringReader stream = new StringReader(ds.GetXml());
            if (mustCloseConnection)
            {
                command.Connection.Close();
            }


            return new XmlTextReader(stream);
        }


        /// <summary>
        /// Provider specific code to set up the updating/ed event handlers used by UpdateDataset
        /// </summary>
        /// <param name="dataAdapter">DataAdapter to attach the event handlers to</param>
        /// <param name="rowUpdatingHandler">The handler to be called when a row is updating</param>
        /// <param name="rowUpdatedHandler">The handler to be called when a row is updated</param>
        protected override void AddUpdateEventHandlers(IDbDataAdapter dataAdapter, RowUpdatingHandler rowUpdatingHandler, RowUpdatedHandler rowUpdatedHandler)
        {
            if (rowUpdatingHandler != null)
            {
                this.m_rowUpdating = rowUpdatingHandler;
                ((MySqlDataAdapter)dataAdapter).RowUpdating += new MySqlRowUpdatingEventHandler(RowUpdating);
            }


            if (rowUpdatedHandler != null)
            {
                this.m_rowUpdated = rowUpdatedHandler;
                ((MySqlDataAdapter)dataAdapter).RowUpdated += new MySqlRowUpdatedEventHandler(RowUpdated);
            }
        }


        /// <summary>
        /// Handles the RowUpdating event
        /// </summary>
        /// <param name="obj">The object that published the event</param>
        /// <param name="e">The SqlRowUpdatingEventArgs</param>
        protected void RowUpdating(object obj, MySqlRowUpdatingEventArgs e)
        {
            base.RowUpdating(obj, e);
        }


        /// <summary>
        /// Handles the RowUpdated event
        /// </summary>
        /// <param name="obj">The object that published the event</param>
        /// <param name="e">The SqlRowUpdatedEventArgs</param>
        protected void RowUpdated(object obj, MySqlRowUpdatedEventArgs e)
        {
            base.RowUpdated(obj, e);
        }


        /// <summary>
        /// Handle any provider-specific issues with BLOBs here by "washing" the IDataParameter and returning a new one that is set up appropriately for the provider.
        /// </summary>
        /// <param name="connection">The IDbConnection to use in cleansing the parameter</param>
        /// <param name="p">The parameter before cleansing</param>
        /// <returns>The parameter after it's been cleansed.</returns>
        protected override IDataParameter GetBlobParameter(IDbConnection connection, IDataParameter p)
        {
            // do nothing special for BLOBs...as far as we know now.
            return p;
        }
        #endregion
    }

标签:ADOHelper,object,new,IDbCommand,command,MySql,override,ADO,public
From: https://blog.51cto.com/u_14751752/5868391

相关文章

  • 基于mysql实现group by 取分组第一条 最后一条
    测试数据DROPTABLEIFEXISTS`tb_dept`;CREATETABLE`tb_dept`(`id`bigint(20)UNSIGNEDNOTNULL,`parent_id`bigint(20)NULLDEFAULTNULL,`dept_cod......
  • mysql8导入数据慢解决
    前提是mysql使用innodb引擎 先优化配置文件:innodb_buffer_pool_size=12Ginnodb_log_file_size  =1G#只能修改配置文件生效innodb_flush_log_at_trx_commit=......
  • Unix/Linux编程(MySQL数据库系统)
    1MySQL介绍MySQL是一个关系数据库系统在关系数据库中,数据存储在表中。每个表由多个行和列组成。表中的数据相互关联。表也可能与其他表有关联。关系结构使得可在表上运行......
  • mysql触发器 增删改
    --------------------------------eb_system_menus触发器编写--新增、修改、删除--自动执行eb_seller_menus--------------------------------delimiter$-......
  • 本地mysql端口3306 一直干不掉这样解决
    第一步:先whereis  mysql(查找到MySQL的一些本地文件)    主要删除这两个  再干掉端口3306 即可 ......
  • mysql无法启动且没有报任何错误
    1.大家使用mysql用了一段时间是不是会发现在任务管理器中启动mysql也不行,用cmd输入mysqlstartmysql和管理员身份以及在安装的bin目录下输入cmd来启动mysql启动无法成功......
  • Mysql : 出现Table ‘./mysql/proc’ is marked as crashed and should be repaired
    今天升级脚本出现的问题:Table‘./mysql/proc’ismarkedascrashedandshouldberepaired一般这种表崩溃的问题出现在mysql异常停止,或者使用kill-9命令强行杀掉进......
  • centos7 在线安装mysql 8.0
      1.下载mysqlrpm包安装源   https://dev.mysql.com/downloads/repo/yum/ (如图,注意选择对应的linux版本,文件10k左右;  或者右键复制链接地址,在服务......
  • 不惧寒冬:20岁女孩创办400亿美元平台,正在击败Adobe、挑战微软
    不惧寒冬:20岁女孩创办400亿美元平台,正在击败Adobe、挑战微软投递人 itwriter 发布于2022-11-1614:56 评论(0) 有1311人阅读 原文链接 [收藏] « »最近......
  • MySQL中的多表操作
    MySQL多表操作1、联合查询联合查询:union,是指将多个查询结果合并成一个结果显示,联合查询是针对查询结果的合并(多条select语句合并)基本语法select查询[决定字段......