使用了事务处理,怎么一个事务中更新两次表就变的相当慢,(ACCESS)比较奇怪,第一次遇见这样的问题头疼了。
        public bool UpdateKindQueueOrder(string _id, string _kindTable, string _direction)
        {
            OleDbParameter[] paras;
            OleDbParameter para;
            string sqlStr, orderId, maxOrderId, minOrderId, kindId;
            try
            {
                paras = new OleDbParameter[2];
                orderId = _accessHelper.SearchId("SELECT order_id FROM " + _kindTable + " where ID=" + _id);
                if (orderId.Length > 0 && _direction == "UP")
                {
                    minOrderId = _accessHelper.SearchId("SELECT max(order_id) FROM " + _kindTable + " where Order_id<" + orderId);
                    kindId = _accessHelper.SearchId("SELECT id FROM " + _kindTable + " where Order_id=" + minOrderId);                    _accessHelper.BeginTran();
                    sqlStr = "UPDATE " + _kindTable + " SET order_id=@orderId where ID=@kindId";
                    para = new OleDbParameter("@orderId", orderId);
                    paras[0] = para;
                    para = new OleDbParameter("@kindId", kindId);
                    paras[1] = para;
                    _accessHelper.ExecSQL(sqlStr, paras);                    para = new OleDbParameter("@orderId", minOrderId);
                    paras[0] = para;
                    para = new OleDbParameter("@kindId", _id);
                    paras[1] = para;
                    _accessHelper.ExecSQL(sqlStr, paras);                    _accessHelper.CommitTran();                    return true;
                }
                else if (orderId.Length > 0 && _direction == "DOWN")
                {
                    maxOrderId = _accessHelper.SearchId("SELECT min(order_id) FROM " + _kindTable + " where Order_id>" + orderId);
                    kindId = _accessHelper.SearchId("SELECT id FROM " + _kindTable + " where Order_id=" + maxOrderId);                    _accessHelper.BeginTran();
                    sqlStr = "UPDATE " + _kindTable + " SET order_id=@orderId where ID=@kindId";
                    para = new OleDbParameter("@orderId", orderId);
                    paras[0] = para;
                    para = new OleDbParameter("@kindId", kindId);
                    paras[1] = para;
                    _accessHelper.ExecSQL(sqlStr, paras);                    para = new OleDbParameter("@orderId", maxOrderId);
                    paras[0] = para;
                    para = new OleDbParameter("@kindId", _id);
                    paras[1] = para;
                    _accessHelper.ExecSQL(sqlStr, paras);                    _accessHelper.CommitTran();                    return true;                }
                else
                {
                    return false;
                }            }
            catch (Exception ex)
            {
                MessageBox.Show("调整顺序时出错!" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                _accessHelper.RollbackTran();
                return false;
            }        }

解决方案 »

  1.   

    使用到的accessHelper的代码:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.OleDb;
    using System.Reflection;
    using System.IO;namespace ITelite.DBUtility
    {
        public class AccessHelper:IDisposable
        {
            private static string strAppDir =Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            private string _connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+strAppDir+@"\DB\CodeManager.mdb";
            private OleDbConnection _conn;
            private OleDbTransaction _tran;
            private OleDbCommand _cmd;
            private bool _isCon = false;
            private bool _isTran = false;        public bool ConState 
            {
                get 
                {
                    return _isCon;
                }
            }        /// <summary>
            /// 构造方法
            /// </summary>
            public AccessHelper() { }        /// <summary>
            /// 构造方法
            /// </summary>
            /// <param name="connectionstr">指定连接字符串</param>
            public AccessHelper(string connectionstr)
            {
                connectionstr = this._connectionString;
            }        /// <summary>
            /// 打开数据库连接
            /// </summary>
            public void ConectionDB()
            {
                try
                {
                    if (!this._isCon)
                    {
                        this._conn = new OleDbConnection();
                        this._conn.ConnectionString = this._connectionString;
                        this._conn.Open();
                    }
                    if (this._cmd == null)
                    {
                        this._cmd = new OleDbCommand();
                        this._cmd.Connection = this._conn;
                    }                this._isCon = true;
                }
                catch (OleDbException ex)
                {
                    this._isCon = false;
                    throw ex;
                }        }        /// <summary>
            /// 开始事务
            /// </summary>
            public void BeginTran()
            {
                try
                {
                    if (!this._isCon) this.ConectionDB();
                    this._tran = this._conn.BeginTransaction(IsolationLevel.ReadCommitted);
                    this._cmd.Transaction = this._tran;
                    this._isTran = true;
                }
                catch (OleDbException ex)
                {
                    this._isTran = false;
                    throw ex;
                }
            }        /// <summary>
            /// 结束事务
            /// </summary>
            public void CommitTran()
            {
                try
                {
                    if (this._isTran)
                    { //有进行开始事务
                        this._tran.Commit();
                        this._isTran = false;
                    }
                }
                catch (OleDbException ex)
                {
                    this._tran.Rollback();
                    throw ex;
                }
            }        /// <summary>
            ///回滚事务
            /// </summary>
            public void RollbackTran()
            {
                try
                {
                    if (this._isTran && this._isCon )
                    { //有进行开始事务
                        this._tran.Rollback();
                        this._isTran = false;
                    }
                }
                catch (OleDbException ex)
                { 
                    throw ex;
                }
            }        /// <summary>
            /// 关闭数据库连接
            /// </summary>
            public void CloseDB()
            {
                try
                {
                    if (this._isCon && this._conn.State != ConnectionState.Closed) this._conn.Close();
                }
                catch (OleDbException ex)
                {
                    throw ex;
                }
            }        
            /// <summary>
            /// 执行修改、添加、删除语句
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <param name="ps">参数数组</param>
            /// <returns>bool返回执行是否成功</returns>
            public bool ExecSQL(string sql, OleDbParameter[] ps)
            {
                try
                {
                    if (this._isCon == false) this.ConectionDB();
                    this._cmd.CommandText = sql;
                    this._cmd.CommandType = CommandType.Text;
                    this._cmd.Parameters.Clear();
                    this._cmd.Parameters.AddRange(ps);
                    return this._cmd.ExecuteNonQuery() > 0;
                }
                catch (OleDbException ex) { 
                    throw ex; }
            }        /// <summary>
            /// 执行修改、添加、删除语句
            /// </summary>
            /// <param name="sql">sql语句</param> 
            /// <returns>bool返回执行是否成功</returns>
            public bool ExecSQl(string sql)
            {
                try
                {
                    if (this._isCon == false) this.ConectionDB();
                    this._cmd.CommandText = sql;
                    return this._cmd.ExecuteNonQuery() > 0;
                }
                catch (OleDbException ex) { throw ex; }
            }        /// <summary>
            /// 释放内存
            /// </summary>
            public void Dispose()
            {
                this.Dispose(true);
                GC.SuppressFinalize(true);
            }        public virtual void Dispose(bool bDispose)
            {
                if (!bDispose)
                {
                    return;
                }
                if (this._isCon)
                {
                    if (this._conn.State != ConnectionState.Closed)
                    {
                        this._conn.Close();
                        this._conn = null;
                        this._cmd = null;
                    }                this._tran = null;
                    this._isCon = false;
                    this._isTran = false;
                }
            }
        }}
    大家帮忙分析一下,望高手指点。
      

  2.   

    你多加些console信息,看看问题出在哪里
      

  3.   

    ok,let me try
    代码方面有问题吗?