一、查詢商品成功
查詢商品.PNG
二、在登錄后的界面直接修改或刪除信息旗国。
修改.PNG
三、具體步驟:
1導(dǎo)入命名空間;
2 運(yùn)用Connection對(duì)象建立與數(shù)據(jù)庫(kù)連接注整;
3 打開連接能曾;
4 利用Command對(duì)象的ExecuteReader()方法執(zhí)行Select查詢語句;
5 利用ExecuteReader()方法返回的DataReader對(duì)象讀取數(shù)據(jù)肿轨,顯示到界面上寿冕;
6關(guān)閉連接。
流程圖.PNG
關(guān)鍵代碼:
int supplier = int.parse(this.cbb supplier.selectedvalue.tostring());
SUPPLIER=@supplier
cmd.parameters.Add(new sqlparameter("@supplier",supplier));
四萝招、DataGridView數(shù)據(jù)綁定流程:
快速搭建商超管理系統(tǒng)數(shù)據(jù)庫(kù)SuperMarketSales:
方法:在數(shù)據(jù)庫(kù)服務(wù)器上蚂斤,新建SuperMarketSales數(shù)據(jù)庫(kù)存捺,并導(dǎo)入SuperMarketSales.sql腳本
五槐沼、重要代碼:查詢:
// 連接字符串,注意與實(shí)際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫(kù)
sqlConn.Open();
// DataGridView數(shù)據(jù)綁定
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫(kù)錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
// 構(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();
// 自定義一個(gè)表(MyGoods)來標(biāo)識(shí)數(shù)據(jù)庫(kù)的GOODS表
adp.Fill(ds, "MyGoods");
// 指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表
this.dgv_Goods.DataSource = ds.Tables["MyGoods"];