根据qnty值

时间:2016-01-13 06:49:12

标签: asp.net

我有两个表table1和table2。我需要从table1到table2插入表值。表1包含列nnum,code,qnty和table2也是相同的列。我需要将table1值插入table2并根据qnty值重复插入。但我当前的代码无法正常工作。在这里,我附上了我需要的格式。图像中的Table2是我需要得到的东西。

 protected void Button15_Click3(object sender, EventArgs e)
{
   string connetionString = null;
            SqlConnection connection ;
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            int i = 0;
            int j = 0;
            connetionString = "Data Source=xxxxx;Initial Catalog=xxxxx;User ID=xxxxx;Password=xxxxxx";
            connection = new SqlConnection(connetionString);
            try
            {
                connection.Open();
                adapter.SelectCommand = new SqlCommand("insert into Table2(Code, Model, Num, Qty) select Code, Model, Num, Qty from Table1", connection);
                adapter.Fill(ds);
                connection.Close();

                String str2 = "select * from Table2;";
                SqlCommand xp2 = new SqlCommand(str2, con);
                con.Open();
                SqlDataAdapter da2 = new SqlDataAdapter();
                da2.SelectCommand = xp2;
                DataSet ds2 = new DataSet();
                da2.Fill(ds2, "Table2");
                con.Close();

                    for (i = 0; i <= Convert.ToInt32(ds2.Tables[0].Rows[i]["Qty"].ToString()) ; i++)
                    {
                        String str1 = "insert into Table2(Code, Model, Num, Qty) select Code, Model, Num, Qty from Table1;";
                        SqlCommand xp1 = new SqlCommand(str1, con);

                        con.Open();
                        SqlDataAdapter da1 = new SqlDataAdapter();
                        da1.SelectCommand = xp1;
                        DataSet ds1 = new DataSet();
                        da1.Fill(ds1, "Code");
                        GridView1.DataSource = ds1;
                        con.Close();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }

}    enter image description here

1 个答案:

答案 0 :(得分:0)

在sql程序中使用过程方法或在程序中使用javascript