标签:.net ado.net dataset c# sql server 2008 winform
2,添加客户和修改客户模块
关于客户的添加模块和修改模块均使用的是一个窗体,其其区别是根据向窗体中所传递的参数来实现的,这个模块的客户名称,性别和重要程度是必填项,当添加客户时,其主要功能就是向CustomerManagement数据库的customer数据表中插入客户的基本信息;当更改客户信息时,其主要的功能就是更改CustomerManagement数据库中的customer数据表中的一条数据记录。
添加或修改客户模块的示例:
(1)加载公共类和创建ID属性,代码为:
<span style="font-size:18px;"> common cm = new common();//加载公共类common database db = new database();//加载公共类database private int _id; public int ID//ID属性用于区分是添加还是修改客户资料 { get//取属性值 { return _id; } set { _id = value;//设置属性值 } }</span>
添加ID属性,这个属性非常重要,它用来区分是添加客户,还是修改客户。
(2)编写根据ID显示客户资料的代码为:
<span style="font-size:18px;"> private void showChildren1(int t)//根据ID显示客户资料 { SqlDataReader dr; db.runSql("select * from customer where id="+t,out dr); if (dr.Read())//显示出客户信息 { tbName.Text = dr["useName"].ToString(); cm.cbbIndexByText(cbbNation, dr["nation"].ToString()); dtpBrithday.Value = DateTime.Parse(dr["brithday"].ToString()); cm.cbbIndexByText(cbbSex,dr["sex"].ToString()); tbCard.Text = dr["IDcard"].ToString(); tbLove.Text = dr["love"].ToString(); cm.cbbIndexByText(cbbMarriage,dr["marriage"].ToString()); cm.cbbIndexByText(cbbConcernment, dr["source"].ToString()); cm.cbbIndexByText(cbbLevel, dr["levels"].ToString()); cm.cbbIndexByText(cbbEarning, dr["earning"].ToString()); cm.cbbIndexByText(cbbSource, dr["concernment"].ToString()); tbMobile.Text = dr["mobile"].ToString(); tbTel.Text = dr["tel"].ToString(); tbQQ.Text = dr["QQ"].ToString(); cm.cbbIndexByText(cbbKultur,dr["Kultur"].ToString()); tbEmail.Text = dr["Email"].ToString(); cm.cbbIndexByText(cbbProvince,dr["province"].ToString()); cm.cbbIndexByText(cbbCity,dr["city"].ToString()); tbAddress.Text = dr["address"].ToString(); dtpAddTime.Value = DateTime.Parse(dr["addTime"].ToString()); } else { MessageBox.Show("你所要修改的项不存在,系统将关闭此窗体!"); Close(); } dr.Close(); } </span>
完整窗体代码为:
<span style="font-size:18px;">using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient;//引入的命名空间 namespace 客户管理系统 { public partial class addChildren1 : Form { common cm = new common();//加载公共类common database db = new database();//加载公共类database private int _id; public int ID//ID属性用于区分是添加还是修改客户资料 { get//取属性值 { return _id; } set { _id=value;//设置属性值 } } public addChildren1()//构造函数 { InitializeComponent(); } private void btn_close_Click(object sender, EventArgs e)//关闭按钮的Click事件 { Close();//关闭窗体 } private void addChildren1_Load(object sender, EventArgs e) { try { cbbNation.SelectedIndex=0;//选择第一项 cbbMarriage.SelectedIndex = 0;//选择第一项 cbbSource.SelectedIndex = 0;//选择第一项 cbbKultur.SelectedIndex = 0;//选择第一项 cbbConcernment.SelectedIndex = 0;//选择第一项 cbbLevel.SelectedIndex = 0;//选择第一项 if (_id != 0) { Text = "客户管理信息修改";//设置窗体标签 btn_add.Text = "修改";//设置按钮标签 showChildren1(_id);//显示数据 } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString());//显示出错信息 } } private void showChildren1(int t)//根据ID显示客户资料 { SqlDataReader dr; db.runSql("select * from customer where id="+t,out dr); if (dr.Read())//显示出客户信息 { tbName.Text = dr["useName"].ToString(); cm.cbbIndexByText(cbbNation, dr["nation"].ToString()); dtpBrithday.Value = DateTime.Parse(dr["brithday"].ToString()); cm.cbbIndexByText(cbbSex,dr["sex"].ToString()); tbCard.Text = dr["IDcard"].ToString(); tbLove.Text = dr["love"].ToString(); cm.cbbIndexByText(cbbMarriage,dr["marriage"].ToString()); cm.cbbIndexByText(cbbConcernment, dr["source"].ToString()); cm.cbbIndexByText(cbbLevel, dr["levels"].ToString()); cm.cbbIndexByText(cbbEarning, dr["earning"].ToString()); cm.cbbIndexByText(cbbSource, dr["concernment"].ToString()); tbMobile.Text = dr["mobile"].ToString(); tbTel.Text = dr["tel"].ToString(); tbQQ.Text = dr["QQ"].ToString(); cm.cbbIndexByText(cbbKultur,dr["Kultur"].ToString()); tbEmail.Text = dr["Email"].ToString(); cm.cbbIndexByText(cbbProvince,dr["province"].ToString()); cm.cbbIndexByText(cbbCity,dr["city"].ToString()); tbAddress.Text = dr["address"].ToString(); dtpAddTime.Value = DateTime.Parse(dr["addTime"].ToString()); } else { MessageBox.Show("你所要修改的项不存在,系统将关闭此窗体!"); Close(); } dr.Close(); } private void btn_add_Click(object sender, EventArgs e)//btn_add按钮的Click事件 { if (tbName.Text.Trim().Length<=0)//判断是否输入用户名 { MessageBox.Show("请输入用户名称!"); tbName.Focus();//获取焦点 btn_add.DialogResult = DialogResult.None; return; } if (cbbSex.SelectedIndex<0)//判断是否选择了性别 { MessageBox.Show("请选择性别!"); cbbSex.Focus();//获取焦点 btn_add.DialogResult = DialogResult.None; return; } if (cbbEarning.SelectedIndex < 0)//判断是否选择了重要程度 { MessageBox.Show("请选择重要程度!"); cbbEarning.Focus();//获取焦点 btn_add.DialogResult = DialogResult.None; return; } if (tbMobile.Text.Trim().Length<=0||tbTel.Text.Trim().Length<=0)//判断手机号码和联系电话号码是否至少填写了一个 { MessageBox.Show("请将电话号码和手机号码至少填写一个!"); tbMobile.Focus();//获取焦点 btn_add.DialogResult = DialogResult.None; return; } string sql = ""; string Rstatus = "添加"; if (_id==0)//添加客户信息的字符串 { sql="insert into customer(userName,nation,brithday,sex,IDcard,love,marriage,source,levels,earning,concernment,mobile,tel,QQ,Kultur,Email,province,city,address,addTime)values" +"('"+tbName.Text.Trim()+"','"+cbbNation.Text+"','"+dtpBrithday.Value+"','"+cbbSex+"','"+tbCard.Text.Trim()+ "','"+tbLove.Text.Trim()+"','"+cbbMarriage.Text+"','"+cbbConcernment.Text+"','"+cbbLevel.Text+ "','"+cbbEarning.SelectedItem+"','"+cbbSource.Text+"','"+tbMobile.Text.Trim()+"','"+ tbTel.Text.Trim()+"','"+tbQQ.Text.Trim()+"','"+cbbKultur.Text+"','"+tbEmail.Text.Trim()+ "','"+cbbProvince.Text+"','"+cbbCity.Text+"','"+tbAddress.Text.Trim()+"','"+dtpAddTime.Value+"')"; } else//修改客户信息的字符串 { Rstatus="修改"; sql="update customer set userName='"+ tbName.Text.Trim()+"',nation='"+ cbbNation.Text+"',brithday='"+ dtpBrithday.Value+"',sex='"+ cbbSex.Text+"',IDcard='"+ tbCard.Text.Trim()+"',love='"+ tbLove.Text.Trim()+"',marriage='"+ cbbMarriage.Text+"',source='"+ cbbConcernment.Text+"',levels='"+ cbbLevel.Text.Trim()+"',earning='"+ cbbEarning.Text.Trim()+"',concernment='"+ cbbSource.Text.Trim()+"',mobile='"+ tbMobile.Text.Trim()+"',tel='"+ tbTel.Text.Trim()+"',QQ='"+ tbQQ.Text.Trim()+"',Kultur='"+ cbbKultur.Text.Trim()+"',Email='"+ tbEmail.Text.Trim()+"',province='"+ cbbProvince.Text.Trim()+"',city='"+ cbbCity.Text.Trim()+"',address='"+ tbAddress.Text.Trim()+"',addTime='"+ dtpAddTime.Value+"'where id="+_id; } db.runSql(sql);//执行sql语句 MessageBox.Show(Rstatus+"成功!"); } } } </span>
运行整个程序的结果为;
添加客户窗体:
修改客户信息资料窗体:
标签:.net ado.net dataset c# sql server 2008 winform
原文地址:http://blog.csdn.net/erlian1992/article/details/46331691