133 lines
3.5 KiB
C#
133 lines
3.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
//using System.Data.OracleClient;
|
||
using System.Data.SqlClient;
|
||
using System.Data.OleDb;
|
||
using DeiNiu.Utils;
|
||
|
||
namespace DeiNiu
|
||
{
|
||
[Serializable]
|
||
public class Connection
|
||
{
|
||
|
||
//static string sqlcon_str = System.Configuration.ConfigurationManager.AppSettings["SqlConnectionString"];
|
||
|
||
private static string _ConnectionString = null;
|
||
|
||
private static string sqlcon_str
|
||
{
|
||
get
|
||
{
|
||
if (_ConnectionString == null)
|
||
_ConnectionString = ConString();// System.Configuration.ConfigurationManager.AppSettings["SqlConnectionString"];
|
||
return _ConnectionString;
|
||
}
|
||
}
|
||
|
||
//private OracleConnection _OrclCon = null;
|
||
private SqlConnection _sqlCon = null;
|
||
// private OleDbConnection _oledbCon = null;
|
||
public SqlConnection getSqlCon()
|
||
{
|
||
try
|
||
{
|
||
_sqlCon = new SqlConnection(sqlcon_str);
|
||
_sqlCon.Open();
|
||
return _sqlCon;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
|
||
/*
|
||
public OracleConnection getOraCon()
|
||
{
|
||
try
|
||
{
|
||
_OrclCon = new OracleConnection(oracon_str);
|
||
_OrclCon.Open();
|
||
return _OrclCon;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
|
||
|
||
public OleDbConnection getOleCon()
|
||
{
|
||
try
|
||
{
|
||
//oledb_str = "Provider=OraOLEDB.Oracle.1;Password=welcome;Persist Security Info=True;User ID=hradmin;Data Source=cpor";
|
||
_oledbCon = new OleDbConnection(oledb_str);
|
||
_oledbCon.Open();
|
||
return _oledbCon;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
*/
|
||
|
||
/// <summary>
|
||
/// 数据库连接字符串,自动判断加密状态并获取对应连接字符串。
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static string ConString()
|
||
{
|
||
if (!string.IsNullOrEmpty(WmsConstants.SQL_CONN_WMS))
|
||
{
|
||
return WmsConstants.SQL_CONN_WMS;
|
||
}
|
||
|
||
if (IsEncrypt())
|
||
{
|
||
string cstr = System.Configuration.ConfigurationManager.AppSettings["SqlConnectionString"];
|
||
try
|
||
{
|
||
//解密后的数据库连接字符串
|
||
cstr = DeiNiu.Utils.DESEncrypt.Decrypt(cstr);
|
||
return cstr;
|
||
}
|
||
catch
|
||
{
|
||
//如果勾选加密,第一次登陆时取到的是未加密连接字符串,所以直接返回。如果返回空字符串会导致首次登陆失败。
|
||
//ConfigurationManager.AppSettings["ConnectionString"] = string.Empty;
|
||
//return string.Empty;
|
||
return cstr;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//无须解密的数据库连接字符串
|
||
return System.Configuration.ConfigurationManager.AppSettings["SqlConnPlatform"];
|
||
}
|
||
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 验证是否已加密
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
internal static bool IsEncrypt()
|
||
{
|
||
switch (System.Configuration.ConfigurationManager.AppSettings["ConStringEncrypt"])
|
||
{
|
||
case "1":
|
||
case "TRUE":
|
||
case "true":
|
||
return true;
|
||
default:
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
}
|