获取到待插入的数据后,如何在存储过程中批量插入?参数如何传递?

解决方案 »

  1.   

    下面这个例子希望给楼主启发。1,单条插入 INSERT INTO time_by_day  
    (time_id, the_date, the_year, month_of_year, quarter,day_of_month) VALUES ('1101', '1999-10-1', '1999', '10', 'Q4','1') 
    2,单条插入: 
    INSERT INTO time_by_day       (time_id, the_date, the_year, month_of_year, quarter, day_of_month) SELECT TOP 1 time_id + 1 AS time_id, the_date + 1 AS the_date, YEAR(the_date + 1)       AS the_year, MONTH(the_date + 1) AS month_of_year, { fn QUARTER(the_date + 1)       } AS quarter, DAY(the_date + 1) AS day_of_month FROM time_by_day ORDER BY time_id DESC 
    3,循环插入:   DECLARE @MyCounter INT SET @MyCounter = 0            /*设置变量*/ WHILE (@MyCounter < 2)     /*设置循环次数*/ BEGIN WAITFOR DELAY '000:00:10'   /*延迟时间10秒*/ INSERT INTO time_by_day       (time_id, the_date, the_year, month_of_year, quarter, day_of_month) SELECT TOP 1 time_id + 1 AS time_id, the_date + 1 AS the_date, YEAR(the_date + 1)       AS the_year, MONTH(the_date + 1) AS month_of_year, { fn QUARTER(the_date + 1)       } AS quarter, DAY(the_date + 1) AS day_of_month FROM time_by_day ORDER BY time_id DESC 
    SET @MyCounter = @MyCounter + 1 END 
    4,插入以时间为变量的数据 
    DECLARE @MyCounter INT declare @the_date datetime SET @MyCounter = 0 SET @the_date = '1999-1-4' WHILE (@MyCounter < 200000) BEGIN WAITFOR DELAY '000:00:10' /*INSERT INTO time_by_day       (time_id, the_date, the_year, month_of_year, quarter, day_of_month) SELECT TOP 1 time_id + 1 AS time_id, the_date + 1 AS the_date, YEAR(the_date + 1)       AS the_year, MONTH(the_date + 1) AS month_of_year, { fn QUARTER(the_date + 1)       } AS quarter, DAY(the_date + 1) AS day_of_month FROM time_by_day ORDER BY time_id DESC */ insert into time_by_day (time_id,the_date)values('371',@the_date) SET @the_date = @the_date + 1 SET @MyCounter = @MyCounter + 1 END 
      

  2.   

    如果是楼主是想那种,先通过程序取得数据保存在DataTable,然后再想通过存储过程,把这个DataTable中的数据批量添加,这是比较困难,一般的做法是把取得数据的SQL语句当参数传给存储过程,再批量复制添加的。
      

  3.   

    System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection("连接字符串");
                System.Data.SqlClient.SqlCommand cm = new System.Data.SqlClient.SqlCommand();
                    
                cm.Connection = cnn;
                cm.CommandText = "select * from table1";
                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cm);            DataTable dt = new DataTable();
                da.Fill(dt);
                da.FillSchema(dt,System.Data.SchemaType.Mapped);//            
                //下面插入多条数据
                for(int i=0;i<10;i++)
                {
                    DataRow newRow = dt.NewRow();
                    newRow[0] = 123;
                    newRow[1] = "abc";
                    //....
                    dt.Rows.Add(newRow);
                }
                //下面重新写回数据库
                System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(da);//
                cnn.Open();
                da.Update(dt);
                cnn.Close();            dt.AcceptChanges();上面的批量更新的方法同样适用于存储过程,只不过,这时候就不要用SqlCommandBuilder了,而是分别为da创建使用存储过程的UpdateCommand、InsertCommand和DeleteCommand