C#中使用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();

猜你喜欢

转载自blog.csdn.net/ABC13222880223/article/details/82112308