一、主要功能
-
錄入商品信息
 -
修改密碼
lh2.gif
二、功能描述
利用庫管員身份登錄到管理界面,單擊錄入商品信息菜單按鈕即可彈出錄入商品信息窗口,并可以進行錄入商品信息,根據(jù)信息錄入完成后單擊錄入按鈕,然后彈出通知窗口錄入成功后才錄入成功,否則錄入失敗。
利用庫管員或者收銀員身份登錄到管理界面,不僅可以錄入商品信息還可以修改登錄密碼,單擊菜單按鈕修改密碼就會彈出一個修改用戶密碼窗口,輸入用戶名以及新密碼和確認密碼后,單擊確定按鈕,通知窗口顯示修改成功即成功修改登錄密碼,否則修改失敗。
三、ADO.NET插入數(shù)據(jù)庫的流程
步驟
1)在"解決方案資源管理器"中的"引用"條目上右鍵添
System.Configuration;
2)修改項目的App.config文件,添加如下彩色代碼內(nèi)容;
<connectionStrings>
<add name="SuperMarketSales"
connectionString="Data Source=.;Initial Catalog=SuperMarketSales;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
3)在需要連接數(shù)據(jù)庫的窗口代碼中添加
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數(shù)據(jù)綁定流程
1)指定ComboBox的數(shù)據(jù)源為DataSet的MySupplier表;
2)將DataSet和DataAdapter綁定;
3)加入鏈接數(shù)據(jù)庫代碼try··catch·· finally;
private void Form1_Load(object sender, EventArgs e)
{
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
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)來標識數(shù)據(jù)庫的SUPPLIER表
adp.Fill(ds, "MySupplier");
// 指定ComboBox的數(shù)據(jù)源為DataSet的MySupplier表
this.comboBox1.DataSource = ds.Tables["MySupplier"];
this.comboBox1.DisplayMember = "NAME"; // ComboBox下拉列表顯示的內(nèi)容,這里顯示供應商名稱
this.comboBox1.ValueMember = "CODE"; // ComboBox另外還攜帶一個隱藏的值叫ValueMember,指定為供應商代碼
this.comboBox1.SelectedIndex = 0;
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯誤:" + 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;
}
// 連接字符串,注意與實際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
// 構造命令
String sqlStr = "update EMPLOYEE set PASSWORD=@pwd where ID=@id";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串參數(shù)賦值
cmd.Parameters.Add(new SqlParameter("@pwd", newPwd));
cmd.Parameters.Add(new SqlParameter("@id", UserInfo.userId));
// 將命令發(fā)送給數(shù)據(jù)庫
int res = cmd.ExecuteNonQuery();
// 根據(jù)返回值判斷是否修改成功
if (res != 0)
{
MessageBox.Show("密碼修改成功");
this.Close();
}
else
{
MessageBox.Show("密碼修改錯誤");
}
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯誤:" + 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();
// 連接字符串,注意與實際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
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字符串參數(shù)賦值
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));
// 將命令發(fā)送給數(shù)據(jù)庫
int res = cmd.ExecuteNonQuery();
// 根據(jù)返回值判斷是否插入成功
if (res != 0)
{
MessageBox.Show("商品信息錄入成功");
}
else
{
MessageBox.Show("商品信息錄入失敗");
}
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯誤:" + exp.ToString());
}
finally
{
sqlConn.Close();
}
}
}
- 庫管員界面單擊錄入商品信息按鈕代碼
// 錄入商品信息
private void tsmi_Record_Click(object sender, EventArgs e)
{
RecordForm recordForm = new RecordForm();
recordForm.MdiParent = this;
recordForm.Show();
}

