可以分组添加,可以进行注册码控制,防止数据泄露,通过双击,实现快速远程,特别是连锁企业门店较多的远程维护管理,非常方便。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Win32;
using System.Configuration;
using System.Runtime.InteropServices;
using System.IO;
using System.Data.SQLite;
namespace sunlogin_assistants
{
public partial class frmMain : Form
{
public DataTable dtgroup;
public DataTable dtaddr;
SoftReg softReg = new SoftReg();
public string rcode, strip, strdbname, strdbusername, strpassword, strSQL;
public int gpint;
public static string connectionString = "Data Source = " AppDomain.CurrentDomain.BaseDirectory "sunlogin helper.db" ";Pooling=true;FailIfMissing=false";
private SQLiteConnection myConnection;
private SQLiteCommandBuilder sqlCmdBld;
private DataSet ds = new DataSet();
private SQLiteDataAdapter da;
public frmMain()
{
InitializeComponent();
}
#region 变量声明区
public string str = "";//该变量保存INI文件所在的具体物理位置
public string strOne = "";
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
int nSize,
string lpFileName);
public string ContentReader(string area, string key, string def)
{
StringBuilder stringBuilder = new StringBuilder(1024);
GetPrivateProfileString(area, key, def, stringBuilder, 1024, str);
return stringBuilder.ToString();
}
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(
string mpAppName,
string mpKeyName,
string mpDefault,
string mpFileName);
#endregion
private void frmMain_Load(object sender, EventArgs e)
{
try
{
str = Application.StartupPath "\\sunlogin assistants.dll";
//此方法也可通过:str = System.AppDomain.CurrentDomain.BaseDirectory @"ConnectString.ini";
strOne = System.IO.Path.GetFileNameWithoutExtension(str);
if (File.Exists(str))
{
rcode = ContentReader(strOne, "regcode", "");
}
string regcd = softReg.GetRNum();
if (rcode == regcd)
{
this.Text = "向日葵远程辅助工具已注册!";
this.btnReg.Enabled = false;
btnAdd.Enabled = true;
btnSave.Enabled = true;
btnInsert.Enabled = true;
btnDelete.Enabled = true;
dgvAdddress.Enabled = true;
pictureBox1.Visible = false;
pictureBox2.Visible = false;
label8.Visible = false;
label9.Visible = false;
btnReg.Visible = false;
btnClose.Visible = false;
GetData("select 门店编码,门店名称,别名,向日葵ID,密码,分组,序号 from raddr order by 门店编码,别名", dgvAdddress);
GetData("select 分组代码,分组名称 from tbGroup order by 分组代码", dgvGroup);
dgvAdddress.Columns[6].Visible = false;
dtgroup = SelectDataBase("select 分组代码,分组名称 from tbGroup");
cbGroup.DataSource = dtgroup;
cbGroup.DisplayMember = "分组名称";
cbGroup.ValueMember = "分组代码";
dgvAdddress.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
dgvGroup.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
else
{
this.btnReg.Enabled = true;
btnAdd.Enabled = false;
btnSave.Enabled = false;
btnInsert.Enabled = false;
btnDelete.Enabled = false;
dgvAdddress.Enabled = false;
btnAddG.Enabled = false;
btnSaveG.Enabled = false;
btnFindG.Enabled = false;
btnDeleteG.Enabled = false;
this.Text = "向日葵远程辅助工具未注册!";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
txtCode.Text = "";
txtName.Text = "";
txtAlias.Text = "";
txtSunId.Text = "";
txtPassword.Text = "";
if (IntSelectDataBase("select count(*) from raddr") >= 0)
{
dtaddr = SelectDataBase("select ifnull(max(序号),0) 序号 from raddr");
txtSeq.Text = Convert.ToString(Convert.ToInt32(dtaddr.Rows[0]["序号"].ToString()) 1);
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
string nodecode, nodename, mdalias, sunid, passwd;
int inssecces, iseq;
nodecode = txtCode.Text.Trim();
nodename = txtName.Text.Trim();
mdalias = txtAlias.Text.Trim();
sunid = txtSunId.Text.Trim();
passwd = txtPassword.Text.Trim();
if (nodecode == "" || nodename == "" || mdalias == "" || sunid == "" || passwd == "")
{
MessageBox.Show("请填写完整再操作!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
iseq = Convert.ToInt32(txtSeq.Text.Trim());
inssecces = IntSelectDataBase("select count(*) from raddr where 门店编码='" txtCode.Text "' and 序号=" iseq);
if (inssecces > 0 && txtSeq.Text.Trim() != "")
{
if (UpdateDataBase("update raddr set 门店名称='" nodename "',向日葵ID='" sunid "',密码='" passwd "',分组='" cbGroup.Text "' where 门店编码='" nodecode "' and 序号=" iseq) > 0)
{
MessageBox.Show("修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
GetData("select 门店编码,门店名称,别名,向日葵ID,密码,分组,序号 from raddr order by 门店编码,别名", dgvAdddress);
}
}
else
{
if (UpdateDataBase("insert into raddr(序号,别名,门店编码,门店名称,分组,向日葵ID,密码) values('" iseq "','" mdalias "','" nodecode "','" nodename "','" cbGroup.Text "','" sunid "','" passwd "')") > 0)
{
MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
GetData("select 分组代码,分组名称 from tbGroup order by 分组代码", dgvGroup);
GetData("select 门店编码,门店名称,别名,向日葵ID,密码,分组,序号 from raddr order by 门店编码,别名", dgvAdddress);
}
}
}
RefreshcbGroup();
}
private void dgvAdddress_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
string psunid, ppasswd;
if (dgvAdddress.RowCount > 0)
{
psunid = dgvAdddress.CurrentRow.Cells[3].Value.ToString();
ppasswd = dgvAdddress.CurrentRow.Cells[4].Value.ToString();
try
{
Process p = Process.Start("C:\\Program Files (x86)\\Oray\\SunLogin\\SunloginClient\\SunloginClient.exe", " --mod=fastcontrol --fastcode=" psunid " --pwd=" ppasswd);
p.WaitForExit();
}
catch (Exception d)
{
MessageBox.Show(d.Message "", "提示!!!!");
}
}
}
private void dgvAdddress_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvAdddress.RowCount > 0)
{
txtCode.Text = dgvAdddress.CurrentRow.Cells[0].Value.ToString();
txtName.Text = dgvAdddress.CurrentRow.Cells[1].Value.ToString();
txtAlias.Text = dgvAdddress.CurrentRow.Cells[2].Value.ToString();
txtSunId.Text = dgvAdddress.CurrentRow.Cells[3].Value.ToString();
txtPassword.Text = dgvAdddress.CurrentRow.Cells[4].Value.ToString();
cbGroup.Text = dgvAdddress.CurrentRow.Cells[5].Value.ToString();
txtSeq.Text = dgvAdddress.CurrentRow.Cells[6].Value.ToString();
}
}
private void dgvGroup_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvAdddress.RowCount > 0)
{
string sgroup;
sgroup = dgvGroup.CurrentRow.Cells[1].Value.ToString();
txtGroupId.Text = dgvGroup.CurrentRow.Cells[0].Value.ToString();
txtGroupName.Text = dgvGroup.CurrentRow.Cells[1].Value.ToString();
if (sgroup != "")
{
cbGroup.SelectedValue = txtGroupId.Text;
GetData("select 门店编码,门店名称,别名,向日葵ID,密码,分组,序号 from raddr where 分组='" sgroup "' order by 门店编码,别名", dgvAdddress);
}
}
}
private void txtCode_KeyPress(object sender, KeyPressEventArgs e)
{
string scode, sname;
scode = txtCode.Text.Trim();
sname = txtName.Text.Trim();
GetData("select 门店编码,门店名称,别名,向日葵ID,密码,分组,序号 from raddr where 门店编码 like '" scode "%' and 门店名称 like '" sname "%' order by 门店编码,别名", dgvAdddress);
}
private void btnInsert_Click(object sender, EventArgs e)
{
string scode, sname;
scode = txtCode.Text.Trim();
sname = txtName.Text.Trim();
GetData("select 门店编码,门店名称,别名,向日葵ID,密码,分组,序号 from raddr where 门店编码 like '" scode "%' and 门店名称 like '" sname "%' order by 门店编码,别名", dgvAdddress);
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (txtCode.Text.Trim() != "" && txtSeq.Text.Trim() != "")
{
if (IntSelectDataBase("select count(*) from raddr where 门店编码='" txtCode.Text.Trim() "' and 序号=" txtSeq.Text.Trim()) > 0)
{
UpdateDataBase("delete from raddr where 门店编码='" txtCode.Text.Trim() "' and 序号=" txtSeq.Text.Trim());
MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
GetData("select 门店编码,门店名称,别名,向日葵ID,密码,分组,序号 from raddr order by 门店编码,别名", dgvAdddress);
}
}
else
{
MessageBox.Show("查询到要删除的数据,再删除!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void dgvAdddress_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 3)
{
if (e.Value != null && e.Value.ToString().Length > 0)
{
e.Value = new string('*', e.Value.ToString().Length);
}
}
if (e.ColumnIndex == 4)
{
if (e.Value != null && e.Value.ToString().Length > 0)
{
e.Value = new string('*', e.Value.ToString().Length);
}
}
}
private void dgvAdddress_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
DataGridView dgv = sender as DataGridView;
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4, e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font, rectangle, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
private void btnReg_Click(object sender, EventArgs e)
{
FormRegister frmRegister = new FormRegister();
frmRegister.ShowDialog();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
#region 数据操作方法
//根据输入的SQL语句检索数据库数据
public DataSet SelectDataBase(string tempStrSQL, string tempTableName)
{
this.strSQL = tempStrSQL;
this.myConnection = new SQLiteConnection(connectionString);
this.da = new SQLiteDataAdapter(this.strSQL, this.myConnection);
this.ds.Clear();
this.da.Fill(ds, tempTableName);
return ds;//返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名
}
//数据库数据更新(传DataSet和DataTable的对象)
public DataSet UpdateDataBase(DataSet changedDataSet, string tableName)
{
this.myConnection = new SQLiteConnection(connectionString);
this.da = new SQLiteDataAdapter(this.strSQL, this.myConnection);
this.sqlCmdBld = new SQLiteCommandBuilder(da);
this.da.Update(changedDataSet, tableName);
return changedDataSet;//返回更新了的数据库表
}
///////////////////////////////// 直接操作数据库(未创建该类的实例时直接用) /////////////////////////////////////////////////////
//检索数据库数据(传字符串,直接操作数据库)
public DataTable SelectDataBase(string tempStrSQL)
{
this.myConnection = new SQLiteConnection(connectionString);
DataSet tempDataSet = new DataSet();
this.da = new SQLiteDataAdapter(tempStrSQL, this.myConnection);
this.da.Fill(tempDataSet);
return tempDataSet.Tables[0];
}
public DataTable GetDgvToTable(DataGridView dgv)
{
DataTable dt = new DataTable();
for (int count = 0; count < dgv.Columns.Count; count )
{
DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString());
dt.Columns.Add(dc);
}
for (int count = 0; count < dgv.Rows.Count; count )
{
DataRow dr = dt.NewRow();
for (int countsub = 0; countsub < dgv.Columns.Count; countsub )
{
dr[countsub] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value);
}
dt.Rows.Add(dr);
}
return dt;
}
//数据库数据更新(传字符串,直接操作数据库)
public int UpdateDataBase(string tempStrSQL)
{
this.myConnection = new SQLiteConnection(connectionString);
//使用Command之前一定要先打开连接,后关闭连接,而DataAdapter则会自动打开关闭连接
myConnection.Open();
SQLiteCommand tempSqlCommand = new SQLiteCommand(tempStrSQL, this.myConnection);
int intNumber = tempSqlCommand.ExecuteNonQuery();//返回数据库中影响的行数
myConnection.Close();
return intNumber;
}
//数据库数据查询(传数量,直接操作数据库)
public int IntSelectDataBase(string tempStrSql)
{
this.myConnection = new SQLiteConnection(connectionString);
myConnection.Open();
SQLiteCommand tempSqlCommand = new SQLiteCommand(tempStrSql, this.myConnection);
int intNumber = Convert.ToInt32(tempSqlCommand.ExecuteScalar());
myConnection.Close();
return intNumber;
}
public void GetData(string sqlCmd, DataGridView dgv)
{
SQLiteDataAdapter sda = new SQLiteDataAdapter(sqlCmd, connectionString);
SQLiteCommandBuilder cmdbd = new SQLiteCommandBuilder(sda);
DataTable dt = new DataTable();
sda.Fill(dt);
dgv.DataSource = dt;
}
#endregion
private void btnAddG_Click(object sender, EventArgs e)
{
txtGroupId.Text = "";
txtGroupName.Text = "";
if (IntSelectDataBase("select count(*) from tbGroup") >= 0)
{
dtaddr = SelectDataBase("select ifnull(max(分组代码),0) 序号 from tbGroup");
txtGroupId.Text = Convert.ToString(Convert.ToInt32(dtaddr.Rows[0]["序号"].ToString()) 1);
}
}
private void btnFindG_Click(object sender, EventArgs e)
{
string scode, sname;
scode = txtCode.Text.Trim();
sname = txtName.Text.Trim();
GetData("select 分组代码,分组名称 from tbGroup where 分组代码 like '" scode "%' and 分组名称 like '" sname "%' order by 分组代码", dgvGroup);
}
private void btnSaveG_Click(object sender, EventArgs e)
{
string gcode, gname;
int inssecces, iseq;
gcode = txtGroupId.Text.Trim();
gname = txtGroupName.Text.Trim();
if (gcode == "" || gname == "")
{
MessageBox.Show("新增时请填写完整再保存,修改时请选择分组修改后再保存!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
iseq = Convert.ToInt32(txtGroupId.Text.Trim());
inssecces = IntSelectDataBase("select count(*) from tbGroup where 分组代码=" iseq);
if (inssecces > 0 && txtGroupName.Text.Trim() != "")
{
if (UpdateDataBase("update tbGroup set 分组名称='" gname "' where 分组代码=" gcode) > 0)
{
MessageBox.Show("修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
GetData("select 分组代码,分组名称 from tbGroup order by 分组代码", dgvGroup);
GetData("select 门店编码,门店名称,别名,向日葵ID,密码,分组,序号 from raddr order by 门店编码,别名", dgvAdddress);
}
}
else if (inssecces == 0 && txtGroupName.Text.Trim() != "")
{
if (UpdateDataBase("insert into tbGroup(分组代码,分组名称) values(" gcode ",'" gname "')") > 0)
{
MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
GetData("select 分组代码,分组名称 from tbGroup order by 分组代码", dgvGroup);
GetData("select 门店编码,门店名称,别名,向日葵ID,密码,分组,序号 from raddr order by 门店编码,别名", dgvAdddress);
}
}
}
RefreshcbGroup();
}
private void btnDeleteG_Click(object sender, EventArgs e)
{
if (txtGroupId.Text.Trim() != "")
{
if (IntSelectDataBase("select count(*) from raddr where 分组=(select 分组名称 from tbGroup where 分组代码=" txtGroupId.Text.Trim() ")") > 0)
{
if (MessageBox.Show("分组下有数据,您真的要删除吗?", "此删除不可恢复", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
UpdateDataBase("delete from raddr where 分组=(select 分组名称 from tbGroup where 分组代码=" txtGroupId.Text.Trim() ")");
UpdateDataBase("delete from tbGroup where 分组代码=" txtGroupId.Text.Trim());
MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
UpdateDataBase("delete from tbGroup where 分组代码=" txtGroupId.Text.Trim());
MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
GetData("select 门店编码,门店名称,别名,向日葵ID,密码,分组,序号 from raddr order by 门店编码,别名", dgvAdddress);
GetData("select 分组代码,分组名称 from tbGroup order by 分组代码", dgvGroup);
RefreshcbGroup();
}
else
{
MessageBox.Show("请输入分组代码!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
public void RefreshcbGroup()
{
cbGroup.DataSource = null;
dtgroup = SelectDataBase("select 分组代码,分组名称 from tbGroup");
cbGroup.DataSource = dtgroup;
cbGroup.DisplayMember = "分组名称";
cbGroup.ValueMember = "分组代码";
dgvAdddress.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
dgvGroup.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
}
}
评论