WinForm日常开发心得

一、.企业在开发程式的时候会用到连接字符串,需要用到将Db的密码加密保存于配置文档中。

自己写的SqlHelper类

public class SqlHelper
{
private readonly static string conStr = GetConnetionStrByIp();//根据Ip环境来获取连接字符串

public static string GetConnetionStrByIp()
{
string constr=string.Empty;
string ip = string.Empty;
//Get IP
IPAddress[] ipList = Dns.GetHostAddresses(Dns.GetHostName());
foreach (var item in ipList)
{
if (item.AddressFamily == AddressFamily.InterNetwork)
{
ip = item.ToString();
}
}

// Get ConStr
string[] strArry = ip.Split(new char[] {'.' });
if (strArry[1]=="130")//OA
{
constr= GetDbInfo.getDbMessage("OA");
}
else
{
constr = GetDbInfo.getDbMessage("SFIS");
}
return constr;
}

public static int ExecuteNoneQuery(string sql, CommandType cmdType, params OracleParameter[] ops)
{
using (OracleConnection con = new OracleConnection(conStr))
{
using (OracleCommand cmd = new OracleCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(ops);
return cmd.ExecuteNonQuery();
}
}

}

public static object ExecuteScale(string sql, CommandType cmdType, params OracleParameter[] ops)
{
using (OracleConnection con = new OracleConnection(conStr))
{
using (OracleCommand cmd = new OracleCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(ops);
return cmd.ExecuteScalar();
}
}
}

public static OracleDataReader ExecuteReader(string sql, CommandType cmdType, params OracleParameter[] ops)
{
OracleConnection con = new OracleConnection(conStr);
using (OracleCommand cmd = new OracleCommand(sql, con))
{
try
{
con.Open();
cmd.Parameters.AddRange(ops);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception e)
{
con.Close();
con.Dispose();
throw (e);
}
}
}

public static DataTable ExecuteGetDataTable(string sql, CommandType cmdType, params OracleParameter[] ops)
{
DataTable dt = new DataTable();
using (OracleDataAdapter oda = new OracleDataAdapter(sql, conStr))
{
if (oda != null)
{
oda.SelectCommand.Parameters.AddRange(ops);
}
oda.Fill(dt);
return dt;
}
}


/// <summary>
/// 存储过程实现分页
/// </summary>
/// <param name="proName">存储过程的名字</param>
/// <param name="pageIndex">当前页码</param>
/// <param name="pageSize">每页多少条</param>
/// <param name="commandType">执行的命令类型</param>
/// <param name="ops">执行命令的参数</param>
/// <returns></returns>
internal static DataSet ExecuteGetDataTableByPro(string proName, CommandType commandType, OracleParameter[] ops)
{
//存储过程执行完成后可能返回多个表的数据,所有这里使用DataSet来接收
DataSet ds = new DataSet();
using (OracleDataAdapter oda = new OracleDataAdapter(proName, conStr))
{
if (oda != null)
{
//设置执行的方式
oda.SelectCommand.CommandType = commandType;
oda.SelectCommand.Parameters.AddRange(ops);
}
oda.Fill(ds);
}
return ds;
}
}

Until类别库中的帮助类:

配置文件读写:

public class IniFileHelper
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);//寫入文件
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder sb, int size, string filePath);//讀文件


//獲取數據
public static string GetValue(string section, string key, string filePath)
{
StringBuilder sb = new StringBuilder(1024);
GetPrivateProfileString(section, key, "", sb, 1024, filePath);
return sb.ToString();
}

//寫入數據
public static void SetValue(string section, string key, string value, string filePath)
{
WritePrivateProfileString(section, key, value, filePath);
}

//刪除對應的節點
public static void DeleteSection(string section, string filePath)
{
WritePrivateProfileString(section, null, null, filePath);
}

//刪除所有的節點
public static void DealeteAllSection(string filePath)
{
WritePrivateProfileString(null, null, null, filePath);
}
}

 

public class EncryptHelper
{
public static string MyEncrypt(string normaltxt)
{
Byte[] buffer = Encoding.Default.GetBytes(normaltxt);
//Byte[] newBuffer=new RSACryptoServiceProvider(new CspParameters()).Encrypt(buffer,false);
return Convert.ToBase64String(buffer);
}

//自定义的加密算法类(简单的加密算法)
public static string MyDecrypt(string securtytxt)
{
try
{
Byte[] buffer = Convert.FromBase64String(securtytxt);
//Byte[] newBuffer = new RSACryptoServiceProvider(new CspParameters()).Decrypt(buffer, false);
return Encoding.UTF8.GetString(buffer);
}
catch (Exception)
{
return string.Empty;
}
}
}

//以上面的两个帮助类为基础,实现根据IP环境获取连接字符串的帮助类

public static string getDbMessage(string dbEnviro)
{
//這個地方加入判斷對應的IP環境獲取對應的DB配置
string dbFilePath = Directory.GetCurrentDirectory() + "\\DBConfig.ini";
string dbIP = IniFileHelper.GetValue(dbEnviro, "IP", dbFilePath);
string dbSSID = IniFileHelper.GetValue(dbEnviro, "SSID", dbFilePath);
string dbPORT = IniFileHelper.GetValue(dbEnviro, "PORT", dbFilePath);
string dbUSER = IniFileHelper.GetValue(dbEnviro, "USER", dbFilePath);
string dbPWD = IniFileHelper.GetValue(dbEnviro, "PWD", dbFilePath);
List<string> list = new List<string>();
list.Add(dbIP);
list.Add(dbSSID);
list.Add(dbPORT);
list.Add(dbUSER);
list.Add(dbPWD);
dbPWD = EncryptHelper.MyDecrypt(dbPWD);//解密

string conStr = "server=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + dbIP + ")(PORT=" + dbPORT + ")))(CONNECT_DATA=(SERVICE_NAME=" + dbSSID + ")));uid=" + dbUSER + ";pwd=" + dbPWD + ";";
return conStr;
}

 

二、关于程式上线的版本控制及程式的单例控制:需要解释的是WinForm程式的入口也是Program类中的Main函数

[STAThread]
static void Main()
{
//獲取當前的進程
Process currProc = Process.GetCurrentProcess();
//獲取當前系統正在運行的進程
Process[] procs = Process.GetProcesses();
List<Process> list = new List<Process>();
foreach (var item in procs)
{
if (item.ProcessName == currProc.ProcessName && item.Id != currProc.Id)
{
list.Add(item);
}
}

if (list.Count > 0)
{
//將之前的窗體顯示處理,并將當前的進程幹掉
Process firstProc = list[0];
IntPtr firstProcWindow = firstProc.MainWindowHandle;
SetForegroundWindow(firstProcWindow);
currProc.Kill();
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Form frm = new Form1();
VersionBll vBll = new VersionBll();
//加載版本信息
string version = Application.ProductVersion.ToString();
string progName = "Change Station";

try
{
//根據當前的版本在程序中去查詢當前可用的版本
string serverVersionStr = vBll.GetServerVersion(progName);
//將版本進行對比,如果一致 直接顯示界面1.0.0.2

Version currentVersion = new Version(version);//當前版本
Version serverVersion = new Version(serverVersionStr);//系統中的版本
if (currentVersion > serverVersion)
{
//更新系統中的版本號為最新
vBll.UpdateVersion(version);
}
else if (currentVersion < serverVersion)
{
ShowNewVersion snv = new ShowNewVersion(version, serverVersionStr);
frm = snv;
//關閉當前顯示
//提示最新版本
}
Application.Run(frm);
}
catch (Exception e)
{
MessageBox.Show("程式開啟出現異常:"+e.Message,"錯誤訊息");//无法连数据库提示信息
}
}

posted @ 2018-12-26 16:47  Francis_Ray  阅读(444)  评论(0编辑  收藏  举报