如何在一个click_button中将每个行ID从第一个表发送到第二个表中的所有行?

时间:2014-05-07 15:03:21

标签: c# asp.net sql-server-2008

我在SQL Server中有两个表,两个表都有大约100多行。如何在一个click_button中将每个行ID从第一个表发送到第二个表中的所有行?感谢。

UPD 有些不合适的东西。 我试图发送QString值(所有需要的行)和回发后选择第一个表的行ID和更新第二个表。主要问题,postback = null后的数据源。

  public string idul;
        public int pro;
        public string nz;
        public string idul2;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                foreach (DataRowView drv in dv)
                {
                    idul = drv["IDUL"].ToString();

                }
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            string path = Server.MapPath(FileUpload1.FileName);
            FileUpload1.SaveAs(Server.MapPath(FileUpload1.FileName));

            Microsoft.Office.Interop.Excel.Application App = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook WB;

            WB = App.Workbooks.Open(path, ReadOnly: true);

            var sheet = (WB.Sheets[1] as Microsoft.Office.Interop.Excel.Worksheet);
            int lastrow = sheet.UsedRange.Columns["A"].Rows.Count();
            int count = sheet.UsedRange.Columns["A:A", Type.Missing].Rows["1:" + lastrow].Count();


            while (count > 0)
            {
                SqlConnection con_ul = new SqlConnection(WebConfigurationManager.ConnectionStrings["JJ"].ConnectionString);
                string ins_ul = "INSERT INTO UL (NZ, Name) VALUES (@NZ, @Name)";
                SqlCommand cmd_ul = new SqlCommand(ins_ul, con_ul);

                cmd_ul.Parameters.AddWithValue("@NZ", pro);
                cmd_ul.Parameters.AddWithValue("@Name", sheet.UsedRange.Cells[count, 1].Value.ToString());

                cmd_ul.Connection.Open();
                cmd_ul.ExecuteNonQuery();
                cmd_ul.Connection.Close();



                SqlConnection con_tsp = new SqlConnection(WebConfigurationManager.ConnectionStrings["JJ"].ConnectionString);
                string ins_tsp = "INSERT INTO TSP (NZ, Name) VALUES (@NZ, @Name)";
                SqlCommand cmd_tsp = new SqlCommand(ins_tsp, con_tsp);

                cmd_tsp.Parameters.AddWithValue("@NZ", pro);
                cmd_tsp.Parameters.AddWithValue("@Name", sheet.UsedRange.Cells[count, 1].Value.ToString());

                cmd_tsp.Connection.Open();
                cmd_tsp.ExecuteNonQuery();
                cmd_tsp.Connection.Close();

                count--;
                pro++;

            }

            App.Quit();
            Label1.Text = "Загруженно " + pro + " записей!";
            System.Threading.Thread.Sleep(200);
            File.Delete(path);
            Button1.Enabled = false;



            Response.Redirect("WebForm1.aspx?pro="+pro);

        }

        protected void Button2_Click(object sender, EventArgs e)
        {


            idul = Request.QueryString["pro"];
            pro = Convert.ToInt32(idul);

            for (int i = pro; i > -1; i--)
            {
                wisp();

            }


        }
        public void wisp()
        {



            SqlConnection con_tsp = new SqlConnection(WebConfigurationManager.ConnectionStrings["JJ"].ConnectionString);
            string tsp1 = "UPDATE TSP SET IDUL=@IDUL WHERE NZ=@NZ";
            SqlCommand upd_tsp = new SqlCommand(tsp1, con_tsp);

            upd_tsp.Parameters.AddWithValue("@NZ", pro);
            upd_tsp.Parameters.AddWithValue("@IDUL", idul);

            upd_tsp.Connection.Open();
            upd_tsp.ExecuteNonQuery();
            upd_tsp.Connection.Close();


            pro--;
            Response.Redirect("WebForm1.aspx?pro=" + pro);


        }

    }
}

0 个答案:

没有答案