Ejemplo n.º 1
0
        public static object ExecuteScalar(OleDbConnection connection, string sqlText)
        {
            lock (_syncRoot)
            {
                try
                {
                    if (connection != null)
                    {
                        if (connection.State == System.Data.ConnectionState.Broken)
                        {
                            connection.Close();
                            connection.Open();
                        }
                        if (connection.State == System.Data.ConnectionState.Closed)
                        {
                            if (connection.State != System.Data.ConnectionState.Connecting)
                            {
                                connection.Open();
                            }
                        }
                        using (var command = connection.CreateCommand())
                        {
                            command.CommandText = sqlText;
                            var val = command.ExecuteScalar();
                            return(val);
                        }
                    }
                }
                catch (SqlException ex)
                {
                    var info = string.Format("执行SQL语句:{0}发生错误,错误信息:{1}", sqlText, ex.Message);
                    LogManager.Log(info);
                    QuestionManager.Add(new Models.Question {
                        Code = "1201", Project = Models.CheckProject.数据库查询, Description = info
                    });
                }
                catch (OleDbException ex)
                {
                    var info = string.Format("执行SQL语句:{0}发生错误,错误信息:{1}", sqlText, ex.Message);
                    LogManager.Log(info);
                    QuestionManager.Add(new Models.Question {
                        Code = "1201", Project = Models.CheckProject.数据库查询, Description = info
                    });
                }

                return(null);
            }
        }