コード例 #1
0
        /// <summary>
        /// 通用更新或删除数据(不同的sql语句)
        /// </summary>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">sql语句或存储过程</param>
        /// <param name="sqlParms">参数集合</param>
        /// <returns></returns>
        public DbExecMsg TOpListData(CommandType cmdType, List <string> cmdText, ref List <SortedList <string, MySqlParameter> > sqlParms)
        {
            DbExecMsg execMsg = new DbExecMsg();

            execMsg.IsSuccess = true;
            execMsg.ErrType   = false;

            using (MysqlDbOp dbOp = new MysqlDbOp(ConnStr))
            {
                try
                {
                    bool isOpera = false;
                    dbOp.BeginTrans();
                    dbOp.DbCmdParm.CmdType = cmdType;
                    int index = 0;
                    foreach (var sqlItem in cmdText)
                    {
                        dbOp.DbCmdParm.CmdText = sqlItem;
                        dbOp.DbCmdParm.MysqlParms.Clear();
                        if (sqlParms != null && sqlParms.Count >= index + 1 && sqlParms[index] != null)
                        {
                            foreach (var pnItem in sqlParms[index].Keys)
                            {
                                dbOp.DbCmdParm.MysqlParms.Add(pnItem, sqlParms[index][pnItem]);
                            }
                        }

                        if (dbOp.BExecNonCmd())
                        {
                            isOpera = true;
                        }
                        else
                        {
                            isOpera = false;
                            break;
                        }

                        index++;
                    }
                    if (isOpera)
                    {
                        dbOp.CommitTrans();
                    }
                    else
                    {
                        dbOp.RollbackTrans();
                    }
                    dbOp.CloseDb();
                }
                catch (Exception ex)
                {
                    execMsg.IsSuccess = false;
                    execMsg.ErrMsg    = ex;
                }
            }

            return(execMsg);
        }
コード例 #2
0
        /// <summary>
        /// 通用获取DataTable
        /// </summary>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">sql语句或存储过程名称</param>
        /// <param name="sqlParms">参数</param>
        /// <param name="dt">返回数据表</param>
        /// <returns></returns>
        public DbExecMsg TGetTable(CommandType cmdType, string cmdText, ref SortedList <string, MySqlParameter> sqlParms, out DataTable dt)
        {
            DbExecMsg execMsg = new DbExecMsg();

            execMsg.IsSuccess = true;
            execMsg.ErrType   = false;
            dt = null;

            using (MysqlDbOp dbOp = new MysqlDbOp(ConnStr))
            {
                dbOp.DbCmdParm.CmdType = cmdType;
                dbOp.DbCmdParm.CmdText = cmdText;
                foreach (var pnItem in sqlParms.Keys)
                {
                    dbOp.DbCmdParm.MysqlParms.Add(pnItem, sqlParms[pnItem]);
                }

                try
                {
                    dt           = dbOp.GetTable();
                    dt.TableName = "Table";
                    //判断是否有业务逻辑错误
                    foreach (var pnItem in dbOp.DbCmdParm.MysqlParms.Keys)
                    {
                        if (sqlParms.ContainsKey(pnItem) && dbOp.DbCmdParm.MysqlParms[pnItem].Direction == ParameterDirection.Output)
                        {
                            if (pnItem == CmdExecErrFlag)
                            {
                                if (!string.IsNullOrEmpty(sqlParms[pnItem].Value.ToString().Trim()))
                                {
                                    execMsg.IsSuccess = false;
                                    execMsg.ErrType   = true;
                                    execMsg.ErrMsg    = new Exception(sqlParms[pnItem].Value.ToString());
                                    break;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    execMsg.IsSuccess = false;
                    execMsg.ErrMsg    = ex;
                }
            }

            return(execMsg);
        }
コード例 #3
0
        /// <summary>
        /// 通用更新或删除数据
        /// </summary>
        /// <param name="cmdType">命令类型</param>
        /// <param name="cmdText">sql语句或存储过程名称</param>
        /// <param name="sqlParms">参数</param>
        /// <param name="opRowCount">返回受影响行数</param>
        /// <returns></returns>
        public DbExecMsg TOpData(CommandType cmdType, string cmdText, ref SortedList <string, MySqlParameter> sqlParms, out int opRowCount)
        {
            DbExecMsg execMsg = new DbExecMsg();

            execMsg.IsSuccess = true;
            execMsg.ErrType   = false;
            opRowCount        = 0;

            using (MysqlDbOp dbOp = new MysqlDbOp(ConnStr))
            {
                dbOp.DbCmdParm.CmdType = cmdType;
                dbOp.DbCmdParm.CmdText = cmdText;
                foreach (var pnItem in sqlParms.Keys)
                {
                    dbOp.DbCmdParm.MysqlParms.Add(pnItem, sqlParms[pnItem]);
                }

                try
                {
                    opRowCount = dbOp.ExecNonCmd();
                    foreach (var pnItem in dbOp.DbCmdParm.MysqlParms.Keys)
                    {
                        if (sqlParms.ContainsKey(pnItem) && dbOp.DbCmdParm.MysqlParms[pnItem].Direction == ParameterDirection.Output)
                        {
                            if (!string.IsNullOrEmpty(sqlParms[pnItem].Value.ToString().Trim()))
                            {
                                execMsg.IsSuccess = false;
                                execMsg.ErrType   = true;
                                execMsg.ErrMsg    = new Exception(sqlParms[pnItem].Value.ToString());
                                opRowCount        = -1;
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    execMsg.IsSuccess = false;
                    execMsg.ErrMsg    = ex;
                }
            }

            return(execMsg);
        }