/// <summary> /// 读取配置文件和测试连接状态 /// </summary> /// <param name="dBType"></param> /// <returns></returns> public bool ReadConfigAndTestConnect() { #region 1.读取配置文件 string configName; if (DataBaseType == DBType.SqlServer) { configName = "MsSqlConfig.py"; } else { configName = "OracleConfig.PY"; } try { ConnString = GetSourStr(new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"CFG\" + configName).ReadLine()); if (ConnString.Trim().Length == 0) { return(false); } } catch (Exception e) { // 读取数据库配置文件出错 SqlLogEvent(false, "读取数据库配置文件出错", e.Message.ToString()); return(false); } #endregion #region 2.测试连接状态 using (SqlConnection_Con conn = new SqlConnection_Con(DataBaseType, ConnString)) { try { conn.DbConnection.Open(); return(true); } catch (Exception e) { SqlLogEvent(false, "测试数据库连接出错", conn.ConnectionString + ":" + e.Message.ToString()); return(false); } finally { conn.DbConnection.Close(); } } #endregion }
/// <summary> /// 返回一个查询的整型值 /// </summary> /// <param name="paramSql">SQL文</param> /// <returns></returns> public int GetIntegerBySql(string paramSql) { using (SqlConnection_Con conn = new SqlConnection_Con(DataBaseType, ConnString)) { using (DbCommandCommon cmd = new DbCommandCommon(DataBaseType)) { PreparCommand(conn.DbConnection, cmd.DbCommand, paramSql, CommandType.Text); SqlLogEvent(true, "执行查询", paramSql); try { DbDataReader dbDataReader; dbDataReader = cmd.DbCommand.ExecuteReader(); if (dbDataReader.Read()) { return(Convert.ToInt32(dbDataReader[0].ToString())); } } catch (Exception exception) { SqlLogEvent(false, "执行查询出错\r\n" + paramSql, exception.Message.ToString()); } return(default);