2.8商品信息查询修改界面功能设计
功能:修改
image功能:删除
imageADO.NET删除数据库流程
image迭代过程
imageDataGridView数据绑定流程
image重要代码
// 连接字符串,注意与实际环境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// DataGridView数据绑定
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.Message);
}
finally
{
sqlConn.Close();
}
// 构造命令
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)来标识数据库的GOODS表
adp.Fill(ds, "MyGoods");
// 指定DataGridView的数据源为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);
}
}
网友评论