同事用C#开发了一个桌面小程序,我把源代码复制转移到另一台电脑上,并配置好了mysql,小程序可以正常打开,但只能查询、浏览数据,不能对数据更新,一点“”保存“”就提示如下内容,恳请各位帮助解决出现的问题?谢谢!
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using SysTool;
using SysTool.DAO;
namespace MainManage
{
public partial class ManageForm2 : Form
{
public ManageForm2()
{
InitializeComponent();
btLast.Enabled = false;
btNext.Enabled = false;
}
List<People> baseData;
People nowControl;
int controlIndex = 0;
/// <summary>
/// 是否操作过简历内容
/// </summary>
bool jianLiFlag = false;
/// <summary>
/// 是否操作过家庭关系
/// </summary>
bool jiaTingFlag = false;
/// <summary>
/// 是否操作过同案内容
/// </summary>
bool tongAnFlag = false;
/// <summary>
/// 操作在押还是离监
/// </summary>
bool ZaiYaLiJian = true;
/// <summary>
/// 是否只读
/// </summary>
bool ReadOnly = false;
public ManageForm2(List<People> baseData,int index,bool zaiYaLiJian)
{
InitializeComponent();
this.baseData = baseData;
controlIndex = index;
nowControl = baseData[index];
ZaiYaLiJian = zaiYaLiJian;
}
public ManageForm2(List<People> baseData, int index, bool zaiYaLiJian,bool readOnly)
{
InitializeComponent();
this.baseData = baseData;
controlIndex = index;
nowControl = baseData[index];
ZaiYaLiJian = zaiYaLiJian;
ReadOnly = readOnly;
}
/// <summary>
/// 设置在押颜色、标签等
/// </summary>
private void zaiYa()
{
tabControl1.TabPages[0].BackColor = SysNormol.ZaiYaColor;
tabControl1.TabPages[1].BackColor = SysNormol.ZaiYaColor;
panelButton.BackColor = SysNormol.ZaiYaColor;
}
/// <summary>
/// 设置离监颜色、标签等
/// </summary>
private void liJian()
{
tabControl1.TabPages[0].BackColor = SysNormol.LiJianColor;
tabControl1.TabPages[1].BackColor = SysNormol.LiJianColor;
panelButton.BackColor = SysNormol.LiJianColor;
Text = "离监罪犯信息查看";
readOnly();
}
/// <summary>
/// 设置只读逻辑
/// </summary>
private void readOnly()
{
btSave.Enabled = false;
btAdd.Enabled = false;
btDelete.Enabled = false;
cmsJianLi.Enabled = false;
cmsJiaTing.Enabled = false;
cmsTongAn.Enabled = false;
}
private void tbShenfen_Leave(object sender, System.EventArgs e)
{
try
{
//当输入完身份证后,自动计算出出生日期
string shenfen = tbShenFen.Text.ToString();
if (!string.IsNullOrEmpty(shenfen))
{
//511102199510258416
//身份证7~14为出生日期
int year = int.Parse(shenfen.Substring(6, 4));
int month = int.Parse(shenfen.Substring(10, 2));
int day = int.Parse(shenfen.Substring(12, 2));
dtpChuSheng.Value = new DateTime(year, month, day);
}
DateTime dt = DateTime.Now;
int peopleYear = (int)dt.Subtract(dtpChuSheng.Value).TotalDays/365;
tbYear.Text = peopleYear.ToString();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void ManageForm_Load(object sender, EventArgs e)
{
if (ZaiYaLiJian)
{
zaiYa();
}
else
{
liJian();
}
if (ReadOnly)
{
readOnly();
}
//如果有当前操作罪犯,依次赋值
//后续增加校验
try
{
if (nowControl != null)
{
loadFormData();
}
else
{
nowControl = new People();
nowControl.isNew = true;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btSave_Click(object sender, EventArgs e)
{
//1.弹出对话框进行确认
DialogResult dr = MessageBox.Show("确认是否保存数据!", "确认", MessageBoxButtons.YesNo,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
if (dr == DialogResult.Yes)
{
try
{
savePeople();
MessageBox.Show("保存成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else if (dr == DialogResult.No)
{
MessageBox.Show("取消保存");
}
}
private void savePeople()
{
//保存数据到nowControl
nowControl.PeopleBirthday = dtpChuSheng.Value;
nowControl.PeopleBoZheng = tbBoZheng.Text;
nowControl.PeopleCengShou = tbCengShou.Text;
nowControl.PeopleDaiBuDate = dtpDaiBuDate.Value;
nowControl.PeopleDaiBuJiGuan = tbDaiBu.Text;
nowControl.PeopleDangan = tbDangAnNumber.Text;
nowControl.PeopleFaJin = tbFaJin.Text;
nowControl.PeopleFanZuiShiShi = tbFanZuiShiShi.Text;
nowControl.PeopleHunYin = cbHunYin.Text;
nowControl.PeopleJiGuan = tbJiGuan.Text;
nowControl.PeopleJuLiuDate = dtpJuLiuDate.Value;
nowControl.PeopleKouYin = tbKouYin.Text;
nowControl.PeopleMinZu = tbMinZu.Text;
nowControl.PeopleName = tbName.Text;
nowControl.PeopleName2 = tbName2.Text;
nowControl.PeopleNumber = tbNumber.Text;
nowControl.PeoplePanJueDate = dtpPanJueDate.Value;
nowControl.PeoplePanJueWenShu = tbPanJueZiHao.Text;
nowControl.PeopleRemark = tbRemark.Text;
nowControl.PeopleRuJianDate = dtpRujian.Value;
nowControl.PeopleShenFen = tbShenFen.Text;
nowControl.PeopleTechang = tbTeChang.Text;
nowControl.PeopleWenhua = tbWenHua.Text;
nowControl.PeopleXingQi = tbXingQi.Text;
nowControl.PeopleXingQiBianGeng = tbXingQiBianGeng.Text;
nowControl.PeopleXingQiEnd = dtpXingQiEnd.Value;
nowControl.PeopleXingQiStart = dtpXingQiStart.Value;
nowControl.PeopleYuanPan = tbFaYuan.Text;
nowControl.PeopleZhengZhi = cbZhengZhi.Text;
nowControl.PeopleZhiye = tbZhiYe.Text;
nowControl.PeopleZhuZhi = tbAddress.Text;
nowControl.PeopleZuiMing = tbZuiMing.Text;
nowControl.PeopleJunJingShi = cbJunJing.Checked;
nowControl.PeopleXiDu = cbXiDu.Checked;
nowControl.PeopleIsSanLei = cbSanLei.Checked; //是否三类犯
nowControl.PeopleLeiFan = cbLei.Checked;
nowControl.PeopleGuanFan = cbGuan.Checked;
nowControl.PeopleSheHei = cbHei.Checked;
nowControl.PeopleSheE = cbE.Checked;
nowControl.PeopleBaoLi = cbBaoLi.Checked;
nowControl.PeopleShenTi = tbShenTi.Text;
//释放时间逻辑
{
if (string.IsNullOrEmpty(nowControl.PeopleXingQiBianGeng))
{
nowControl.PeopleShiFangDate = nowControl.PeopleXingQiEnd;
}
}
//根据nowControl数据保存到数据库,调用数据库操作类
DBFunction.SavePeopleInfo(nowControl, jianLiFlag, jiaTingFlag, tongAnFlag);
//返回执行结果并显示
nowControl.isNew = false;
}
private void btAdd_Click(object sender, EventArgs e)
{
//新增
nowControl = new People();
nowControl.isNew = true;
//清空数据
{
tbNumber.Text = string.Empty;
tbDangAnNumber.Text = string.Empty;
tbName.Text = string.Empty;
tbName2.Text = string.Empty;
tbShenFen.Text = string.Empty;
cbHunYin.Text = "未婚";
tbTeChang.Text = string.Empty;
tbZhiYe.Text = string.Empty;
tbWenHua.Text = string.Empty;
tbJiGuan.Text = string.Empty;
tbKouYin.Text = string.Empty;
cbZhengZhi.Text = "群众";
dtpChuSheng.Value = DateTime.Now;
tbMinZu.Text = string.Empty;
tbAddress.Text = string.Empty;
tbZuiMing.Text = string.Empty;
tbBoZheng.Text = string.Empty;
tbFaJin.Text = string.Empty;
tbXingQi.Text = string.Empty;
dtpXingQiStart.Value = DateTime.Now;
dtpXingQiEnd.Value = DateTime.Now;
dtpRujian.Value = DateTime.Now;
dtpShifang.Value = DateTime.Now;
tbFaYuan.Text = string.Empty;
tbPanJueZiHao.Text = string.Empty;
dtpPanJueDate.Value = DateTime.Now;
dtpJuLiuDate.Value = DateTime.Now;
dtpDaiBuDate.Value = DateTime.Now;
tbDaiBu.Text = string.Empty;
tbCengShou.Text = string.Empty;
cbJunJing.Checked = false;
cbXiDu.Checked = false;
cbLei.Checked = false;
tbRemark.Text = string.Empty;
tbFanZuiShiShi.Text = string.Empty;
dgvJianli.Rows.Clear();
dgvJiating.Rows.Clear();
dgvTongan.Rows.Clear();
picPeople.Image = null;
}
}
private void tbName_Leave(object sender, EventArgs e)
{
}
private void tsmiTongAnAdd_Click(object sender, EventArgs e)
{
dgvTongan.Rows.Add();
tongAnFlag = true;
}
private void tsmiTongAnDelete_Click(object sender, EventArgs e)
{
int count = 0;
foreach (DataGridViewRow row in dgvTongan.Rows)
{
if (row.Selected)
{
dgvTongan.Rows.Remove(row);
count++;
tongAnFlag = true;
}
}
if (count==0)
{
MessageBox.Show("你没有选择任何行");
}
}
private void tsmJianLiAdd_Click(object sender, EventArgs e)
{
dgvJianli.Rows.Add();
jianLiFlag = true;
}
private void tsmJianLiDelete_Click(object sender, EventArgs e)
{
int count = 0;
foreach (DataGridViewRow row in dgvJianli.Rows)
{
if (row.Selected)
{
dgvJianli.Rows.Remove(row);
count++;
jianLiFlag = true;
}
}
if (count == 0)
{
MessageBox.Show("你没有选择任何行");
}
}
private void tsmJiaTingAdd_Click(object sender, EventArgs e)
{
dgvJiating.Rows.Add("", "","","","","群众","","","","");
jiaTingFlag = true;
}
private void tsmJiaTingDelete_Click(object sender, EventArgs e)
{
int count = 0;
foreach (DataGridViewRow row in dgvJiating.Rows)
{
if (row.Selected)
{
dgvJiating.Rows.Remove(row);
count++;
jiaTingFlag = true;
}
}
if (count == 0)
{
MessageBox.Show("你没有选择任何行");
}
}
private void dgvJiating_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
jiaTingFlag = true;
if (e.RowIndex > -1 && e.ColumnIndex == ColJiaTingBirthday.Index)
{
string strDate = SysNormol.GetObjectString(dgvJiating.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
if (!string.IsNullOrEmpty(strDate))
{
DateTime dt;
if (!DateTime.TryParse(strDate, out dt))
{
MessageBox.Show("请输入正确的时间格式,如yyyy/MM");
}
}
}
}
private void dgvTongan_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
tongAnFlag = true;
if (e.RowIndex > -1 && e.ColumnIndex == ColTonganDate.Index)
{
string strDate = SysNormol.GetObjectString(dgvTongan.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
if (!string.IsNullOrEmpty(strDate))
{
DateTime dt;
if (!DateTime.TryParse(strDate, out dt))
{
MessageBox.Show("请输入正确的时间格式,如yyyy/MM");
}
}
}
}
private void dgvJianli_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
jianLiFlag = true;
if (e.RowIndex>-1&&(e.ColumnIndex==ColStartDate.Index||e.ColumnIndex==ColEndDate.Index))
{
string strDate = SysNormol.GetObjectString(dgvJianli.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
if (!string.IsNullOrEmpty(strDate))
{
DateTime dt;
if(!DateTime.TryParse(strDate, out dt))
{
MessageBox.Show("请输入正确的时间格式,如yyyy/MM");
}
}
}
}
private void dtpXingQiEnd_ValueChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(tbXingQiBianGeng.Text))
{
dtpShifang.Value = dtpXingQiEnd.Value;
}
}
private void tbXingQiBianGeng_MouseLeave(object sender, EventArgs e)
{
try
{
if (!string.IsNullOrEmpty(tbXingQiBianGeng.Text))
{
string strDate = tbXingQiBianGeng.Text.Substring(tbXingQiBianGeng.Text.LastIndexOf("刑止:"));
dtpShifang.Value = DateTime.Parse(strDate);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btNext_Click(object sender, EventArgs e)
{
if (controlIndex==baseData.Count-1)
{
}
else
{
controlIndex++;
nowControl = baseData[controlIndex];
loadFormData();
}
}
//加载界面数据
private void loadFormData()
{
Text = "在押罪犯档案信息-" + nowControl.PeopleName;
tbNumber.Text = nowControl.PeopleNumber; //编号
tbName.Text = nowControl.PeopleName; //姓名
tbYear.Text = nowControl.PeopleYear.ToString();
//照片读取
try
{
string fileName = Directory.GetCurrentDirectory().ToString() + "\\照片库\\" + nowControl.PeoplePhoto;
bool test = File.Exists(fileName);
FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read);
Image photo = Image.FromStream(fs);
picPeople.Image = photo;
fs.Close();
}
catch (Exception ex)
{
//如果读取不到图片
//1.导入图片(后续)
picPeople.Image = null;
}
dgvJianli.Rows.Clear();
dgvJiating.Rows.Clear();
dgvTongan.Rows.Clear();
nowControl = DBFunction.GetPeopleOthers(nowControl);
//本人简历
for (int i = 0; i < nowControl.PeopleJianLiList.Count; i++)
{
PeopleJianLi jl = nowControl.PeopleJianLiList[i];
dgvJianli.Rows.Add(jl.JianLiId, jl.PeopleId, jl.JianLiStart.ToString("yyyy/MM"), jl.JianLiEnd.ToString("yyyy/MM"), jl.JianLiDanWei, jl.JianLiZhiYe);
}
//家庭情况
for (int i = 0; i < nowControl.PeopleJiaTingList.Count; i++)
{
PeopleJiaTing jt = nowControl.PeopleJiaTingList[i];
dgvJiating.Rows.Add(jt.JiaTingId, jt.PeopleId, jt.JiaTingGuanXi, jt.JiaTingName, jt.JiaTingBirthday.ToString("yyyy/MM"), jt.JiaTingZhengZhi,jt.JiaTingDanWei, jt.JiaTingZhiYe,jt.JiaTingAddress,jt.JiaTingDianHua);
}
//同案情况
for (int i = 0; i < nowControl.PeopleTongAnList.Count; i++)
{
PeopleTongAn ta = nowControl.PeopleTongAnList[i];
dgvTongan.Rows.Add(ta.TongAnId, ta.PeopleId, ta.TongAnName, ta.TongAnSex, ta.TongAnBirthday.ToShortDateString(), ta.TongAnZhiYe, ta.TongAnZuiMing, ta.TongAnXingQi, ta.TongAnAddress);
}
btLast.Enabled = !(controlIndex == 0);
btNext.Enabled = !(controlIndex == baseData.Count -1);
if (btLast.Enabled)
{
btLast.Show();
}
else
{
btLast.Hide();
}
if (btNext.Enabled)
{
btNext.Show();
}
else
{
btNext.Hide();
}
}
private void btLast_Click(object sender, EventArgs e)
{
if (controlIndex == 0)
{
}
else
{
controlIndex--;
nowControl = baseData[controlIndex];
loadFormData();
}
}
private void btExit_Click(object sender, EventArgs e)
{
Close();
}
private void btDelete_Click(object sender, EventArgs e)
{
if (nowControl.isNew)
{
MessageBox.Show("罪犯信息未保存,不能进行离监操作!");
return;
}
//离监逻辑
//1.添加进离监数据表
//2.删除在押数据表
//弹出对话框进行确认
DialogResult dr = MessageBox.Show(string.Format("确认对{0}进行离监操作!",tbName.Text), "确认", MessageBoxButtons.YesNo,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
if (!dr.Equals(DialogResult.Yes))
{
return;
}
MyMessage m = DBFunction.PeopleLiJian(nowControl);
if (m.success)
{
MessageBox.Show(string.Format("{0}离监成功!", tbName.Text));
}
else
{
MessageBox.Show(m.message);
}
}
private void dtpXingQiEnd_ValueChanged_1(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(tbXingQiBianGeng.Text))
{
dtpShifang.Value = dtpXingQiEnd.Value;
}
}
private void picPeople_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();//打开文件对话框
openFileDialog.Title = "选择";
openFileDialog.Filter = "图片文件 (*.jpg)|*.jpg;|图片文件 (*.png)|*.png;";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
DialogResult dr = MessageBox.Show(string.Format("确认对{0}进行照片绑定!", tbName.Text), "确认", MessageBoxButtons.YesNo,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
if (!dr.Equals(DialogResult.Yes))
{
return;
}
//save pic file.
File.Copy(openFileDialog.FileName, Directory.GetCurrentDirectory().ToString() + "\\照片库\\" + nowControl.PeoplePhoto,true);
//照片读取
try
{
string fileName = Directory.GetCurrentDirectory().ToString() + "\\照片库\\" + nowControl.PeoplePhoto;
bool test = File.Exists(fileName);
FileStream fs = File.Open(fileName,FileMode.Open,FileAccess.Read);
Image photo = Image.FromStream(fs);
picPeople.Image = photo;
fs.Close();
}
catch (Exception ex)
{
//如果读取不到图片
//1.导入图片(后续)
picPeople.Image = null;
}
}
}
}
}