2.8商品信息查詢修改界面功能設(shè)計(jì)
功能:修改
功能:刪除
ADO.NET刪除數(shù)據(jù)庫流程
迭代過程
DataGridView數(shù)據(jù)綁定流程
重要代碼
// 連接字符串厢塘,注意與實(shí)際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
// DataGridView數(shù)據(jù)綁定
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯(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ù)據(jù)庫的GOODS表
adp.Fill(ds, "MyGoods");
// 指定DataGridView的數(shù)據(jù)源為DataSet的MyGoods表
this.dgv_Goods.DataSource = ds.Tables["MyGoods"];
private void dgv_Goods_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1
&& e.ColumnIndex == 0)
{
string objectId = this.dgv_Goods["Id", e.RowIndex].Value.ToString();
MessageBox.Show(objectId);
}
else if (e.RowIndex != -1
&& e.ColumnIndex == 1)
{
string objectId = this.dgv_Goods["Id", e.RowIndex].Value.ToString();
MessageBox.Show(objectId);
}
}