winform 增删改查
增删改,是一个样的指令:
?//添加物品
??????????? try
??????????? {
??????????????? gmyConn = gmyConn == null ? mySqlConnection.GetConn() : gmyConn;
??????????????? SqlCommand cmd2 = gmyConn.CreateCommand();
??????????????? cmd2.CommandText = string.Format("insert into H_Acticle (ActicleName,ActicleNB,WorkShop,ActicleValidDate,TypeId,ActicleNote,ActicleEnglishName) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", ActicleName, ActicleNB, WorkShop, ActicleValidDate, typeId, ActicleNote, ActicleEnglishName);
??????????????? int i = cmd2.ExecuteNonQuery();//增加,删除,修改
??????????????? if (i == 1)
??????????????? {
??????????????????? MessageBox.Show("添加成功!");
??????????????????? this.DialogResult = DialogResult.OK;
??????????????????? return;
??????????????? }
??????????????? else
??????????????? {
??????????????????? MessageBox.Show("添加失败!");
??????????????????? this.DialogResult = DialogResult.OK;
??????????????????? return;
??????????????? }
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? MessageBox.Show("系统错误" + ex.Message);
??????????? }
?
查询
? try
??????????????? {
??????????????????? gmyConn = gmyConn == null ? mySqlConnection.GetConn() : gmyConn;
??????????????????? //命令对象
??????????????????? SqlCommand cmd = gmyConn.CreateCommand();
??????????????????? cmd.CommandText = "select * from table1";
??????????????????? using (SqlDataReader reader = cmd.ExecuteReader())
??????????????????? {
??????????????????????? if (reader.Read()) //while循环读取数据行,得到是一个集合,如果是if,就是得到的一条数据了
??????????????????????? {
??????????????????????????? typeId = Convert.ToInt32(reader["TypeId"]); //读取当前行的第1列的数据
??????????????????????? }
??????????????????? }
??????????????? }
??????????????? catch (Exception ex)
??????????????? {
??????????????????? MessageBox.Show("系统故障!" + ex.Message);
??????????????????? return;
??????????????? }