将datatable或dataset或datarow[]一次写入数据库

private static void rrrr()
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=school;User ID=sa;pwd=123456");
            con.Open();

            //根据查询语句自动生成与表相对应的delete、update、insert语句
            SqlDataAdapter sd = new SqlDataAdapter("select * from banji", con);
           
            DataTable dt = new DataTable();
           
            sd.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i].RowState != DataRowState.Added)
                {
                    //为每行添加一个更新记号记号
                    dt.Rows[i].SetAdded();
                }
            }

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                Console.WriteLine(dt.Columns[i].ColumnName);
            }

            //需要有SqlCommandBuilder的支持
            SqlCommandBuilder sb = new SqlCommandBuilder(sd);

            sd.Update(dt);
        }

你可能感兴趣的:(将datatable或dataset或datarow[]一次写入数据库)