Files
lserp_cs_6.0/其他程序/Lskj.GetSecretKey/FrmMain.cs
T
2026-07-02 10:11:39 +00:00

180 lines
6.4 KiB
C#

using DevExpress.XtraEditors;
using Lskj.Control;
using Lskj.Core;
using Lskj.Data;
using Lskj.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Lskj.GetSecretKey
{
public partial class FrmMain : BaseForm
{
#region 公有参数
#endregion
public FrmMain()
{
InitializeComponent();
this.txtServerName.Focus();
this.txtDays.KeyDown += new KeyEventHandler(OnKeysDown);
this.txtDBName.KeyDown += new KeyEventHandler(OnKeysDown);
this.txtServerName.KeyDown += new KeyEventHandler(OnKeysDown);
this.LoginName.KeyDown += new KeyEventHandler(OnKeysDown);
this.LoginPwd.KeyDown += new KeyEventHandler(OnKeysDown);
this.setPanel.Click += new EventHandler(OnSetPanel_Click);
}
private void OnSetPanel_Click(object sender, EventArgs e)
{
if (ConnectServer())
{
FrmPsdChange form = new FrmPsdChange();
form.passWord = form.oldPassWord = this.LoginPwd.Text;
form.userName = form.oldUserName = this.LoginName.Text;
DialogResult result = form.ShowDialog();
if (result == DialogResult.OK)
{
this.LoginPwd.Text = form.passWord;
this.LoginName.Text = form.userName;
}
form.Dispose();
}
}
private void btnConnect_Click(object sender, EventArgs e)
{
ConnectServer();
}
private void btnSave_Click(object sender, EventArgs e)
{
ConnectServer(true);
}
/// <summary>
/// <para>说明:连接服务器</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-08-09 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="isSave">if set to <c>true</c> [is save].</param>
private bool ConnectServer(bool isSave = false)
{
string serverName = this.txtServerName.Text.Trim();
string dbName = this.txtDBName.Text.Trim();
if (string.IsNullOrWhiteSpace(serverName))
{
MessageUtil.Show(ResourceKeys.ServerNameIsNull);
return false;
}
if (string.IsNullOrWhiteSpace(dbName))
{
MessageUtil.Show(ResourceKeys.ServerDBIsNull);
return false;
}
if (string.IsNullOrWhiteSpace(this.LoginName.Text.Trim()))
{
MessageUtil.Show("数据库登录名不能为空!");
return false;
}
if (string.IsNullOrWhiteSpace(this.LoginPwd.Text.Trim()))
{
MessageUtil.Show("数据库登录密码不能为空!");
return false;
}
DBConfig.Instance.ServerName = serverName;
DBConfig.Instance.DataBase = dbName;
// 测试数据库连接
try
{
bool isConnection = false;
if (!isSave)
{
string Connection = "Server={0};Database={1};Persist Security Info=True;User ID=" + LoginName.Text + ";Password=" + LoginPwd.Text + ";Connection Timeout=5;MultipleActiveResultSets=true";
Connection = AESUtil.Encrypt(Connection);
SqlHelper._connection = null;
isConnection = DBConfig.Instance.CreateConnection(Connection);
if (!isConnection)
{
MessageUtil.Show(ResourceKeys.ConnectFault);
return false;
}
}
if (isSave)
{
//DBConfig.Instance.WriteConfig();
string args = AESUtil.Encrypt(txtServerName.Text + "^" + txtDBName.Text + "^" + LoginName.Text + "^" + LoginPwd.Text + "^" + (string.IsNullOrEmpty(txtDays.Text) ? "0" : txtDays.Text));
string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemResources.txt");
//创建文件
using (FileStream fs = new FileStream("SystemResources.txt", FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
{
sw.WriteLine(args);
}
}
this.Close();
return true;
}
else
{
MessageUtil.Show(ResourceKeys.ConnectSuccess);
return true;
}
}
catch (Exception)
{
MessageUtil.Show(ResourceKeys.ConnectFault);
return false;
}
}
private void FrmMain_Load(object sender, EventArgs e)
{
string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemResources.txt");
if (File.Exists(file))
{
string args = AESUtil.Decrypt(File.ReadAllText(file));
string[] controlAfgs = args.Split('^');
if (controlAfgs != null && controlAfgs.Length == 5)
{
txtServerName.Text = controlAfgs[0];
txtDBName.Text = controlAfgs[1];
LoginName.Text = controlAfgs[2];
LoginPwd.Text = controlAfgs[3];
txtDays.Text = controlAfgs[4];
}
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void OnKeysDown(object sender, KeyEventArgs e)
{
TextEdit txtEdit = (TextEdit)sender;
if (e.KeyCode == Keys.Enter)
{
if (txtEdit.Name.Equals("LoginPwd"))
{
this.OnSetPanel_Click(sender, e);
}
else
{
SendKeys.Send("{Tab}");
}
}
}
}
}