一、主要功能
-
录入商品信息
-
修改密码
二、功能描述
利用库管员身份登录到管理界面,单击录入商品信息菜单按钮即可弹出录入商品信息窗口,并可以进行录入商品信息,根据信息录入完成后单击录入按钮,然后弹出通知窗口录入成功后才录入成功,否则录入失败。
利用库管员或者收银员身份登录到管理界面,不仅可以录入商品信息还可以修改登录密码,单击菜单按钮修改密码就会弹出一个修改用户密码窗口,输入用户名以及新密码和确认密码后,单击确定按钮,通知窗口显示修改成功即成功修改登录密码,否则修改失败。
三、ADO.NET插入数据库的流程
步骤
1)在"解决方案资源管理器"中的"引用"条目上右键添
System.Configuration;
2)修改项目的App.config文件,添加如下彩色代码内容;
<connectionStrings>
<add name="SuperMarketSales"
connectionString="Data Source=.;Initial Catalog=SuperMarketSales;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
3)在需要连接数据库的窗口代码中添加
using System.Configuration;
4)按如下语法引用连接字符串;
String
connStr=ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
<connectionStrings>
<add name="SuperMarketSales"
connectionString="Data Source=.;Initial Catalog=SuperMarketSales;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
四、ComboBox数据绑定流程
1)指定ComboBox的数据源为DataSet的MySupplier表;
2)将DataSet和DataAdapter绑定;
3)加入链接数据库代码try··catch·· finally;
private void Form1_Load(object sender, EventArgs e)
{
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// 构造查询命令
String sqlStr = "select * from SUPPLIER order by CODE";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// 将该查询过程绑定到DataAdapter
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
// 将DataSet和DataAdapter绑定
DataSet ds = new DataSet();
// 自定义一个表(MySupplier)来标识数据库的SUPPLIER表
adp.Fill(ds, "MySupplier");
// 指定ComboBox的数据源为DataSet的MySupplier表
this.comboBox1.DataSource = ds.Tables["MySupplier"];
this.comboBox1.DisplayMember = "NAME"; // ComboBox下拉列表显示的内容,这里显示供应商名称
this.comboBox1.ValueMember = "CODE"; // ComboBox另外还携带一个隐藏的值叫ValueMember,指定为供应商代码
this.comboBox1.SelectedIndex = 0;
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(this.comboBox1.Text.ToString() + ", " + this.comboBox1.SelectedValue.ToString());
}
五、重要代码及其详细描述
- 收银员界面单击修改密码的click
// 修改密码
private void tsmi_Password_Click(object sender, EventArgs e)
{
PwdForm pwdForm = new PwdForm();
pwdForm.MdiParent = this;
pwdForm.StartPosition = FormStartPosition.CenterScreen;
pwdForm.Show();
}
- 库管员界面单击修改密码的click
// 修改密码
private void tsmi_Password_Click(object sender, EventArgs e)
{
PwdForm pwdForm = new PwdForm();
pwdForm.MdiParent = this;
pwdForm.StartPosition = FormStartPosition.CenterScreen;
pwdForm.Show();
}
- 修改密码的主要代码
// 点击“确认”按钮
private void bt_Ok_Click(object sender, EventArgs e)
{
String userName = this.tb_User.Text.Trim();
String newPwd = this.tb_NewPwd.Text.Trim();
String confPwd = this.tb_ConfirmPwd.Text.Trim();
// 验证输入信息
if (newPwd.Equals(""))
{
MessageBox.Show("请输入新密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (confPwd.Equals(""))
{
MessageBox.Show("请输入确认密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (newPwd != confPwd)
{
MessageBox.Show("两次密码不一致", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 连接字符串,注意与实际环境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// 构造命令
String sqlStr = "update EMPLOYEE set PASSWORD=@pwd where ID=@id";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串参数赋值
cmd.Parameters.Add(new SqlParameter("@pwd", newPwd));
cmd.Parameters.Add(new SqlParameter("@id", UserInfo.userId));
// 将命令发送给数据库
int res = cmd.ExecuteNonQuery();
// 根据返回值判断是否修改成功
if (res != 0)
{
MessageBox.Show("密码修改成功");
this.Close();
}
else
{
MessageBox.Show("密码修改错误");
}
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.ToString());
}
finally
{
sqlConn.Close();
}
}
// 在“新密码”输入框中按“回车”,光标跳转到“确认密码”输入框
private void tb_NewPwd_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
SendKeys.Send("{tab}");
}
}
// 在“确认密码”输入框中按“回车”,则直接修改密码
private void tb_ConfirmPwd_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
this.bt_Ok_Click(sender, e);
}
}
}
- 录入商品的主要代码
// 点击“确认”按钮,则录入商品
private void bt_Ok_Click(object sender, EventArgs e)
{
String id = this.tb_Id.Text.Trim();
String name = this.tb_Name.Text.Trim();
float price = float.Parse(this.tb_Price.Text.Trim());
String spec = this.tb_Spec.Text.Trim();
String remark = this.tb_Remark.Text.Trim();
// 连接字符串,注意与实际环境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// 构造命令
String sqlStr = "insert into GOODS2(ID, NAME, PRICE, SPEC, REMARK) values(@id, @name, @price, @spec, @remark)";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串参数赋值
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.Parameters.Add(new SqlParameter("@name", name));
cmd.Parameters.Add(new SqlParameter("@price", price));
cmd.Parameters.Add(new SqlParameter("@spec", spec));
cmd.Parameters.Add(new SqlParameter("@remark", remark));
// 将命令发送给数据库
int res = cmd.ExecuteNonQuery();
// 根据返回值判断是否插入成功
if (res != 0)
{
MessageBox.Show("商品信息录入成功");
}
else
{
MessageBox.Show("商品信息录入失败");
}
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.ToString());
}
finally
{
sqlConn.Close();
}
}
}
- 库管员界面单击录入商品信息按钮代码
// 录入商品信息
private void tsmi_Record_Click(object sender, EventArgs e)
{
RecordForm recordForm = new RecordForm();
recordForm.MdiParent = this;
recordForm.Show();
}