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();

https://blog.csdn.net/ABC13222880223/article/details/82112308?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-4-82112308-blog-96474588.235v36pc_relevant_anti_vip&spm=1001.2101.3001.4242.3&utm_relevant_index=7

posted @ 2025-01-24 15:36  wenhongshen  阅读(9)  评论(0编辑  收藏  举报