2.9 商品信息查詢修改界面功能設(shè)計
1.效果圖
查詢 修改.gif
2.描述畫面主要功能王悍,并列出后臺數(shù)據(jù)庫表結(jié)構(gòu)
對商品信息進(jìn)行查詢修改
300000 圓珠筆 3 902 1
300001 海飛絲 25 900 中瓶
300002 潘婷 21.2 900 中瓶
300003 訂書機(jī) 13 902 A4
300004 礦泉水 1.5 901 500mL
300005 礦泉水 5 901 2L
300006 佳潔士 8 900 150mL
300007 吉列 25 900 鋒速三
3.ADO.NET刪除數(shù)據(jù)庫的流程
流程圖.PNG
1.導(dǎo)入命名空間
2.定義數(shù)據(jù)庫連接字符串冰抢,運(yùn)行Connection對象建立與數(shù)據(jù)庫連接
3.打開連接
4.利用Command對象的ExecuteNoQuery()方法執(zhí)行Delete語句
5.通過ExecuteNoQuery()方法返回值判斷是否修改成功,并在界面上提示
6.關(guān)閉連接
4.描述迭代過程
無.PNG
有.png
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
// 構(gòu)造查詢命令
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)來標(biāo)識數(shù)據(jù)庫的SUPPLIER表
adp.Fill(ds, "MySupplier");
// 指定ComboBox的數(shù)據(jù)源為DataSet的MySupplier表
this.cbb_Supplier.DataSource = ds.Tables["MySupplier"];
this.cbb_Supplier.DisplayMember = "NAME"; // ComboBox下拉列表顯示的內(nèi)容郑诺,這里顯示供應(yīng)商名稱
this.cbb_Supplier.ValueMember = "CODE"; // ComboBox另外還攜帶一個隱藏的值叫ValueMember,指定為供應(yīng)商代碼
this.cbb_Supplier.SelectedIndex = 0;
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
5.DataGridView數(shù)據(jù)綁定流程
// 指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表
this.dgv_Goods.DataSource = ds.Tables["MyGoods"];
6.重要代碼
修改
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();
// 構(gòu)造命令
String sqlStr = "update GOODS set NAME=@name, PRICE=@price, SPEC=@spec, REMARK=@remark where ID=@id";
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("商品信息修改成功");
this.Close();
}
else
{
MessageBox.Show("商品信息修改失敗");
}
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
private void bt_Cancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
查詢
// 連接字符串含潘,注意與實際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
// 構(gòu)造命令
String sqlStr = "select * from GOODS where 1=1 ";
// 添加查詢條件
if (!this.tb_Id.Text.Trim().Equals("")){
sqlStr += " and ID='" + this.tb_Id.Text.Trim() + "'";
}
if (!this.tb_Name.Text.Trim().Equals("")){
sqlStr += " and NAME like '%" + this.tb_Name.Text.Trim() + "%'";
}
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// 將該查詢過程綁定到DataAdapter
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
// 將DataSet和DataAdapter綁定
DataSet ds = new DataSet();
// 自定義一個表(MyGoods)來標(biāo)識數(shù)據(jù)庫的GOODS表
adp.Fill(ds, "MyGoods");
// 指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表
this.dgv_Goods.DataSource = ds.Tables["MyGoods"];
// DataGridView數(shù)據(jù)綁定
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}