使用Rows.Add和Rows.Remove操作数据

        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=News;Integrated Security=True");
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from stu", con);
        SqlCommandBuilder cmdbld = new SqlCommandBuilder(da);
        DataSet ds = new DataSet();
        da.Fill(ds,"stu1");

        DataRow dr = ds.Tables[0].NewRow();
        dr["SName"] = "小兰";
        dr["SSex"] = "女";
        dr["SAge"] = 16;
        ds.Tables[0].Rows.Add(dr);//添加新行


        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (ds.Tables[0].Rows[i]["SName"].ToString().Equals("熊大da"))
            {
                ds.Tables[0].Rows.RemoveAt(i);//删除表中SName的值为熊大da的行
            }
 
        }

        da.Update(ds,"stu1");//把修改提交到数据库
        con.Close();

你可能感兴趣的:(使用Rows.Add和Rows.Remove操作数据)