Exemple #1
0
        public static List <string> GetFirstRow(EmDbType dbType, string connString, CommandType cmdType, string cmdText,
                                                params DbParameter[] commandParameters)
        {
            if (string.IsNullOrEmpty(connString))
            {
                throw new ArgumentNullException("connString");
            }
            if (string.IsNullOrEmpty(cmdText))
            {
                throw new ArgumentNullException("cmdText");
            }

            try
            {
                var conn = DBFactory.NewConnection(dbType, connString);
                conn.Open();

                return(GetFirstRow(conn, dbType, cmdType, cmdText, commandParameters));
            }
            catch (Exception ex)
            {
                EventLogger.Log("SqlHelper.GetFirstRow 【" + cmdText + "】", ex);
                OpenConnection(dbType, connString);  //重新刷新
                return(null);
            }
        }
Exemple #2
0
        /// <summary>
        /// 执行带参数的SQL语句
        /// </summary>
        /// <param name="dbType"></param>
        /// <param name="connString"></param>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        public static string ExecuteNonQuery(EmDbType dbType, string connString, CommandType cmdType, string cmdText,
                                             params DbParameter[] commandParameters)
        {
            if (string.IsNullOrEmpty(connString))
            {
                throw new ArgumentNullException("connString");
            }
            if (string.IsNullOrEmpty(cmdText))
            {
                throw new ArgumentNullException("cmdText");
            }

            try
            {
                DbConnection conn = DBFactory.NewConnection(dbType, connString);

                return(ExecuteNonQuery(conn, dbType, cmdType, cmdText, commandParameters));
            }
            catch (Exception ex)
            {
                EventLogger.Log($"SqlServerHelper.ExecuteNonQuery SQL[{cmdText}] 错误.", ex);
                OpenConnection(dbType, connString);  //重新连接
                return(ex.Message);
            }
        }
Exemple #3
0
        public static EmDbType MainDbType = EmDbType.None; //主 数据库类型

        #endregion

        #region 0.1 主数据初始化

        /// <summary>
        /// 初始化主连接
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        /// <param name="datasource">数据源</param>
        /// <param name="database">数据库</param>
        /// <param name="userid">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public static bool InitialMainConn(EmDbType dbType, string datasource, string database, string userid, string password)
        {
            string connnString = DBFactory.BuildConnStr(dbType, datasource, database, userid, password);

            MainConnectionString = connnString;
            MainDbType           = dbType;
            MainConn             = DBFactory.NewConnection(dbType, connnString);

            return(InitialMainConn(dbType, connnString));
        }
Exemple #4
0
        /// <summary>
        /// 初始化主连接
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        /// <param name="connnString">连接字符串</param>
        /// <returns></returns>
        public static bool InitialMainConn(EmDbType dbType, string connnString)
        {
            if (string.IsNullOrEmpty(connnString))
            {
                throw new ArgumentNullException("connnString");
            }

            MainConnectionString = connnString;
            MainDbType           = dbType;
            MainConn             = DBFactory.NewConnection(dbType, connnString);

            return(RefreshMainSqlConnection());
        }
Exemple #5
0
        /// <summary>
        /// 打开数据库连接
        /// </summary>
        /// <param name="dbType"></param>
        /// <param name="connString">数据库连接语句</param>
        /// <returns></returns>
        public static bool OpenConnection(EmDbType dbType, string connString)
        {
            try
            {
                DbConnection conn = DBFactory.NewConnection(dbType, connString);
                conn.Open();
                return(true);
            }
            catch (Exception ex)
            {
                string error = $"打开链接失败,连接语句 [{connString}].";
                EventLogger.Log(error, ex);

                return(false);
            }
        }
Exemple #6
0
        public static string ExecuteNonQuerys(EmDbType dbType, string connString, List <string> sqlList)
        {
            if (string.IsNullOrEmpty(connString))
            {
                throw new ArgumentNullException("connString");
            }

            try
            {
                DbConnection conn = DBFactory.NewConnection(dbType, connString);
                return(ExecuteNonQuerys(conn, dbType, sqlList));
            }
            catch (Exception ex)
            {
                EventLogger.Log("SqlHelper.ExecuteNonQuerys 错误.", ex);
                OpenConnection(dbType, connString);  //重新连接
                return(ex.Message);
            }
        }
Exemple #7
0
        /// <summary>
        /// 刷新连接
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static bool RefreshSqlConnection(string connectionString)
        {
            try
            {
                if (!string.IsNullOrEmpty(connectionString))
                {
                    MainConnectionString = connectionString;
                }

                MainConn = DBFactory.NewConnection(MainConnectionString);
                MainConn.Open();
                return(true);
            }
            catch (Exception ex)
            {
                string error = $"打开链接失败,连接语句 [{connectionString}].";
                EventLogger.Log(error, ex);

                return(false);
            }
        }
Exemple #8
0
        public static DbDataReader ExecuterReader(string connectionString, CommandType cmdType, string cmdText,
                                                  params DbParameter[] commandParameters)
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentNullException("connectionString");
            }
            if (string.IsNullOrEmpty(cmdText))
            {
                throw new ArgumentNullException("cmdText");
            }

            try
            {
                var conn = DBFactory.NewConnection(connectionString);
                conn.Open();

                var cmd = DBFactory.NewCommand(conn);
                cmd.CommandType = cmdType;
                cmd.CommandText = cmdText;

                if (commandParameters != null)
                {
                    foreach (var param in commandParameters)
                    {
                        cmd.Parameters.Add(param);
                    }
                }
                var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();

                return(dr);
            }
            catch (Exception ex)
            {
                EventLogger.Log("SqlHelper.ExecuterReader 【" + cmdText + "】", ex);
                RefreshSqlConnection();
                return(null);
            }
        }