获取到待插入的数据后,如何在存储过程中批量插入?参数如何传递?
解决方案 »
- 怎么发布asp.net做的论坛啊
- 请教几个关于DataGrid控件的问题~~~
- 关于用户控件的简单问题
- 如何将取出的值赋给一个字符串,以便在整个网页中调用呢?
- 如何插入新增一行表格?表格的内容格式已定
- 求购.net模拟炒股网站源代码
- 一个DataTable的数据添加到另一个Datatable的问题~~~
- 在发出去的邮件里面,怎么做退订功能?
- 为什么用Request.Form["xxx"]不能得到htm页post过来的<Select Name="xxx">的数据?
- 关于字段判断问题,在DataBider.Eval(Container.Dataitem,"字段")!请各为高手赐教!(急急!!在线等!!有高分!)
- 有没人遇到过这样奇怪的问题?
- 邮件发送程序遇到问题
(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
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