[C#] 纯文本查看 复制代码
// Launcher.GX.SqlHelper
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Launcher.GX.Source_files;
public class SqlHelper
{
private static readonly string PasswordHash;
private static readonly string SaltKey;
private static readonly string VIKey;
private static int Timeout;
public const string BestNet = "MuOnline";
public const string ExtDataBase = "MuOnlineGKEx";
public const string UserInfoCURD = "UserInfoCURD";
public static string BestDataSource;
public static string BestUserId;
public static string BestPwd;
public const int localhost = 0;
public const int IsWindow = 0;
public const int IsUpdate = 1;
public const int IsReaderConfig = 1;
public const int IsArea = 1;
private static Dictionary<string, string> ConnStrs;
static SqlHelper()
{
PasswordHash = "P@@Sw0rd";
SaltKey = "S@LT&KEY";
VIKey = "@1B2c3D4e5F6g7H8";
Timeout = 1000;
BestDataSource = "";
BestUserId = "";
BestPwd = "";
ConnStrs = new Dictionary<string, string>();
INIFile inif = new INIFile(AppDomain.CurrentDomain.BaseDirectory + "LauncherCfg");
Globals.SQLAddress = Decrypt(AES_EnorDecrypt.AESDecrypt(inif.Read("Config", "SQLAddress"), Decrypt(inif.Read("Config", "AESPass"))));
Globals.SQLUserName = Decrypt(AES_EnorDecrypt.AESDecrypt(inif.Read("Config", "SQLUserName"), Decrypt(inif.Read("Config", "AESPass"))));
Globals.SQLUserPass = Decrypt(AES_EnorDecrypt.AESDecrypt(inif.Read("Config", "SQLUserPass"), Decrypt(inif.Read("Config", "AESPass"))));
BestUserId = Globals.SQLUserName;
BestPwd = Globals.SQLUserPass;
bool flag = true;
string cFile = "Config-Dev.ini";
if (File.Exists(cFile))
{
using StreamReader sr = new StreamReader(cFile);
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.IndexOf("IpAddress") >= 0)
{
BestDataSource = line.Split('=')[1];
BestDataSource = BestDataSource.Replace("\"", "");
BestDataSource = BestDataSource.Trim();
break;
}
}
}
if (string.IsNullOrEmpty(BestDataSource))
{
BestDataSource = Globals.SQLAddress;
}
bool flag2 = false;
bool flag3 = false;
ConnStrs.Add("MuOnline", "Data Source=" + BestDataSource + ";Initial Catalog=MuOnline;Persist Security Info=True;User ID=" + BestUserId + ";pwd=" + BestPwd);
ConnStrs.Add("MuOnlineGKEx", "Data Source=" + BestDataSource + ";Initial Catalog=MuOnlineGKEx;Persist Security Info=True;User ID=" + BestUserId + ";pwd=" + BestPwd);
}
private static SqlConnection GetConnection(string database)
{
if (string.IsNullOrEmpty(database))
{
throw new Exception("未设置参数:database");
}
if (!ConnStrs.ContainsKey(database))
{
throw new Exception("未找到数据库:" + database);
}
return new SqlConnection(ConnStrs[database]);
}
private static SqlCommand GetCommand(SqlConnection conn, SqlTransaction transaction, CommandType cmdType, string sql, SqlParameter[] parms)
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = cmdType;
cmd.CommandTimeout = Timeout;
if (transaction != null)
{
cmd.Transaction = transaction;
}
if (parms != null && parms.Length != 0)
{
cmd.Parameters.AddRange(parms);
}
return cmd;
}
public static DataTable QueryDataTable(string database, string sql, SqlParameter[] parms, CommandType cmdType)
{
if (string.IsNullOrEmpty(database))
{
throw new Exception("未设置参数:database");
}
if (string.IsNullOrEmpty(sql))
{
throw new Exception("未设置参数:sql");
}
try
{
using SqlConnection conn = GetConnection(database);
conn.Open();
using SqlCommand cmd = GetCommand(conn, null, cmdType, sql, parms);
using SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
catch (SqlException ex)
{
StringBuilder log = new StringBuilder();
log.Append("查询数据出错:");
log.Append(ex);
throw new Exception(log.ToString());
}
}
public static DataSet QueryDataSet(string database, string sql, SqlParameter[] parms, CommandType cmdType)
{
if (string.IsNullOrEmpty(database))
{
throw new Exception("未设置参数:database");
}
if (string.IsNullOrEmpty(sql))
{
throw new Exception("未设置参数:sql");
}
try
{
using SqlConnection conn = GetConnection(database);
conn.Open();
using SqlCommand cmd = GetCommand(conn, null, cmdType, sql, parms);
using SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
catch (SqlException ex)
{
StringBuilder log = new StringBuilder();
log.Append("查询数据出错:");
log.Append(ex);
throw new Exception(log.ToString());
}
}
public static object QueryScalar(string database, string sql, SqlParameter[] parms, CommandType cmdType)
{
if (string.IsNullOrEmpty(database))
{
throw new Exception("未设置参数:database");
}
if (string.IsNullOrEmpty(sql))
{
throw new Exception("未设置参数:sql");
}
try
{
using SqlConnection conn = GetConnection(database);
conn.Open();
using SqlCommand cmd = GetCommand(conn, null, cmdType, sql, parms);
return cmd.ExecuteScalar();
}
catch (SqlException ex)
{
StringBuilder log = new StringBuilder();
log.Append("处理出错:");
log.Append(ex);
throw new Exception(log.ToString());
}
}
public static int Execute(string database, string sql, SqlParameter[] parms, CommandType cmdType)
{
if (string.IsNullOrEmpty(database))
{
throw new Exception("未设置参数:database");
}
if (string.IsNullOrEmpty(sql))
{
throw new Exception("未设置参数:sql");
}
int count = 0;
try
{
using SqlConnection conn = GetConnection(database);
conn.Open();
using SqlCommand cmd = GetCommand(conn, null, cmdType, sql, parms);
if (cmdType == CommandType.StoredProcedure)
{
cmd.Parameters.AddWithValue("@RETURN_VALUE", "").Direction = ParameterDirection.ReturnValue;
}
count = cmd.ExecuteNonQuery();
if (count <= 0 && cmdType == CommandType.StoredProcedure)
{
count = (int)cmd.Parameters["@RETURN_VALUE"].Value;
}
}
catch (SqlException ex)
{
StringBuilder log = new StringBuilder();
log.Append("处理出错:");
log.Append(ex);
throw new Exception(log.ToString());
}
return count;
}
public static DataTable QueryDataTable(string database, string sql, CommandType cmdType, IDictionary<string, object> values)
{
SqlParameter[] parms = DicToParams(values);
return QueryDataTable(database, sql, parms, cmdType);
}
public static DataSet QueryDataSet(string database, string sql, CommandType cmdType, IDictionary<string, object> values)
{
SqlParameter[] parms = DicToParams(values);
return QueryDataSet(database, sql, parms, cmdType);
}
public static object QueryScalar(string database, string sql, CommandType cmdType, IDictionary<string, object> values)
{
SqlParameter[] parms = DicToParams(values);
return QueryScalar(database, sql, parms, cmdType);
}
public static int Execute(string database, string sql, CommandType cmdType, IDictionary<string, object> values)
{
SqlParameter[] parms = DicToParams(values);
return Execute(database, sql, parms, cmdType);
}
public static SqlParameter[] DicToParams(IDictionary<string, object> values)
{
if (values == null)
{
return null;
}
SqlParameter[] parms = new SqlParameter[values.Count];
int index = 0;
foreach (KeyValuePair<string, object> kv in values)
{
SqlParameter parm = null;
if (kv.Value == null)
{
parm = new SqlParameter(kv.Key, DBNull.Value);
}
else
{
Type t = kv.Value.GetType();
parm = new SqlParameter(kv.Key, NetToSql(kv.Value.GetType()));
parm.Value = kv.Value;
}
parms[index++] = parm;
}
return parms;
}
public static SqlDbType NetToSql(Type t)
{
SqlDbType dbType = SqlDbType.Variant;
switch (t.Name)
{
case "Int16":
dbType = SqlDbType.SmallInt;
break;
case "Int32":
dbType = SqlDbType.Int;
break;
case "Int64":
dbType = SqlDbType.BigInt;
break;
case "Single":
dbType = SqlDbType.Real;
break;
case "Decimal":
dbType = SqlDbType.Decimal;
break;
case "Byte[]":
dbType = SqlDbType.VarBinary;
break;
case "Boolean":
dbType = SqlDbType.Bit;
break;
case "String":
dbType = SqlDbType.NVarChar;
break;
case "Char[]":
dbType = SqlDbType.Char;
break;
case "DateTime":
dbType = SqlDbType.DateTime;
break;
case "DateTime2":
dbType = SqlDbType.DateTime2;
break;
case "DateTimeOffset":
dbType = SqlDbType.DateTimeOffset;
break;
case "TimeSpan":
dbType = SqlDbType.Time;
break;
case "Guid":
dbType = SqlDbType.UniqueIdentifier;
break;
case "Xml":
dbType = SqlDbType.Xml;
break;
case "Object":
dbType = SqlDbType.Variant;
break;
}
return dbType;
}
public static string Decrypt(string encryptedText)
{
byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(32);
RijndaelManaged symmetricKey = new RijndaelManaged
{
Mode = CipherMode.CBC,
Padding = PaddingMode.None
};
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
}
}