DataTable中的数据一次写入到数据库中,也就是只和数据库交互一次,不用循环DataTable一条一条的写入!!!

wszj851218 2009-01-08 02:43:14
DataTable是新建的,里面存放的是读取Excel中的数据,有6000多条。
原来是用循环DataTable来一条一条的插入数据的,这样和数据库交互太频繁,有没有方法一次插入全部???
SQL语句执行的最好!!!
...全文
654 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
ilovethisweb 2009-12-15
  • 打赏
  • 举报
回复
MARK
appleller 2009-07-30
  • 打赏
  • 举报
回复
XUEXI
iishou8 2009-05-08
  • 打赏
  • 举报
回复
mark
huaikong666 2009-01-08
  • 打赏
  • 举报
回复
4楼的方法是正确的 我用过
dqlfjy 2009-01-08
  • 打赏
  • 举报
回复
把数据存到一个DataSet里,存好后再打开数据库,做ds更新,一次存入.
yhy0611 2009-01-08
  • 打赏
  • 举报
回复
一句SQL搞定跟多条SQL一起,连接不是每次都关闭,效率有区别吗?
lovehongyun 2009-01-08
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 wszj851218 的回复:]
我的意思就是想用一句sql搞定,不用循环多次sql交互
[/Quote]

你要是愿意,甚至你可以不写一句sql语句都可以搞定,参看16楼我所说的方法
lovehongyun 2009-01-08
  • 打赏
  • 举报
回复
例如我可以写这样的代码
比如说我要对表
create table table1
(
id int,
name varchar(50)
)
插入10条记录,那么我写成下面就ok了

DataTable dt = new DataTable()
dt.Columns.Add("id",typeof(int));
for(int i =0;i<=10;i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = "test"+i;
dt.Rows.Add(dr);
}

//我得到了dt,不管这个dt是怎么来的.总之得到了(你的可能是excel读出来的),那只要写几行代码就ok了,如下:

foreach(DataRow dr in dt.Rows)
{
dr.SetAdded();
}

da.InsertCommand = 一个insert的command
da.Update(dt);
wszj851218 2009-01-08
  • 打赏
  • 举报
回复
insert into 表名 select 字段 from 表名
其实要是能把DataTable转换为一个表就好了
HDNGO 2009-01-08
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 wszj851218 的回复:]
我的意思就是想用一句sql搞定,不用循环多次sql交互
[/Quote]

怎么才算一条?拼一个2M的insert select union扔上去算一条?
raozhiven 2009-01-08
  • 打赏
  • 举报
回复
11楼正解
wszj851218 2009-01-08
  • 打赏
  • 举报
回复
我的意思就是想用一句sql搞定,不用循环多次sql交互
che2piaopiao 2009-01-08
  • 打赏
  • 举报
回复
帮顶
heroyct 2009-01-08
  • 打赏
  • 举报
回复
循环写sql语句
批处理执行
cwmwss 2009-01-08
  • 打赏
  • 举报
回复
SqlDataAdapter 利用SqlCommandBuilder 直接插入。。。。
用这个11楼的方法cs里代码比较简单。
在数据库里还它还是一条条插入的!
  • 打赏
  • 举报
回复
取表结构用select top 0 * from table 就可以了
lovehongyun 2009-01-08
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 HDNGO 的回复:]
引用 13 楼 lovehongyun 的回复:
4楼复杂我的代码..

4楼的代码即可.

只要把你的datatable的行状态搞成Added,然后sqlDataAdapter.update(dt)即可了.

注意要设置sqlDataAdapter的insertcommand命令


你这个比较通用~我那个只能做DEMO~~

于是就剽窃你的~~

我认为用1 <>1更好一些,你觉得?
[/Quote]

呵呵.我只是逗逗你.
如果是2.0的话.就没必要在用一条select语句去返回一个空的datatable再往里新加数据了.只要遍历table的行.SetAdded()行状态.一下就全搞定了
lovehongyun 2009-01-08
  • 打赏
  • 举报
回复
或者可以用SqlBulkCopy.WriteToServer(DataTable)一次将数据写入数据库
参看下例:

主要看using (SqlBulkCopy bcp =
new SqlBulkCopy(connection))部分代码即可

using System;
using System.Data;
using System.Data.SqlClient;

namespace Microsoft.Samples.SqlServer
{
class Program
{
public static void Main(string[] args)
// Define and open a connection to AdventureWorks.
{
using (SqlConnection connection =
new SqlConnection(GetConnectionString()))
{
connection.Open();
// Perform an initial count on the
// destination table.
SqlCommand commandRowCount = new SqlCommand(
"SELECT COUNT(*) FROM " +
"dbo.BulkCopyDemoMatchingColumns;",
connection);
long countStart = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Starting row count = " +
countStart);
// Create a table with some rows.
DataTable tableNewProducts = MakeTable();
// Set up the bulk copy object.
// Note that the column positions in the
// source data reader match the column
// positions in the destination table so
// there is no need to map columns.
using (SqlBulkCopy bcp =
new SqlBulkCopy(connection))
{
bcp.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns";
// Write from the source to
// the destination.
bcp.WriteToServer(tableNewProducts);
}
// Perform a final count on the destination
// table to see how many rows were added.
long countEnd = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Ending row count = " +
countEnd);
long countAdded = countEnd - countStart;
if (countAdded == 1)
{
Console.WriteLine("1 row was added.");
}
else
{
Console.WriteLine(countAdded +
" rows were added.");
}

Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
}

private static DataTable MakeTable()
// Create a new DataTable named NewProducts.
{
DataTable tableNewProducts =
new DataTable("NewProducts");
// Add three column objects to the table.
DataColumn columnID = new DataColumn();
columnID.DataType =
System.Type.GetType("System.Int32");
columnID.ColumnName = "ProductID";
columnID.AutoIncrement = true;
tableNewProducts.Columns.Add(columnID);
DataColumn columnName = new DataColumn();
columnName.DataType =
System.Type.GetType("System.String");
columnName.ColumnName = "Name";
tableNewProducts.Columns.Add(columnName);
DataColumn columnProductNumber =
new DataColumn();
columnProductNumber.DataType =
System.Type.GetType("System.String");
columnProductNumber.ColumnName =
"ProductNumber";
tableNewProducts.Columns.Add(
columnProductNumber);
// Create an array for DataColumn objects.

DataColumn[] keys = new DataColumn[1];
keys[0] = columnID;
tableNewProducts.PrimaryKey = keys;
// Add some new rows to the collection.
DataRow row;
row = tableNewProducts.NewRow();
row["Name"] = "CC-101-WH";
row["ProductNumber"] = "Cyclocomputer - White";
tableNewProducts.Rows.Add(row);
row = tableNewProducts.NewRow();
row["Name"] = "CC-101-BK";
row["ProductNumber"] = "Cyclocomputer - Black";
tableNewProducts.Rows.Add(row);
row = tableNewProducts.NewRow();
row["Name"] = "CC-101-ST";
row["ProductNumber"] = "Cyclocomputer - Stainless";
tableNewProducts.Rows.Add(row);
tableNewProducts.AcceptChanges();
// Return the new DataTable.
return tableNewProducts;
}

// MARS is turned on in the connection string because this sample
// performs a bulk copy in the same database, using the same connection.
// However, MARS is not required to use the SqlBulkCopy functionality.
private static string GetConnectionString()
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
{
return "Data Source=(local);" +
"Integrated Security=SSPI;" +
"Initial Catalog=AdventureWorks;" +
"MultipleActiveResultSets=True";
}
}
}
HDNGO 2009-01-08
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 lovehongyun 的回复:]
4楼复杂我的代码..

4楼的代码即可.

只要把你的datatable的行状态搞成Added,然后sqlDataAdapter.update(dt)即可了.

注意要设置sqlDataAdapter的insertcommand命令
[/Quote]

你这个比较通用~我那个只能做DEMO~~

于是就剽窃你的~~

我认为用1<>1更好一些,你觉得?
wszj851218 2009-01-08
  • 打赏
  • 举报
回复
但是每次做插入是就会和数据库进行交互,也就会在log中添加数据,这样就会添加很多的日志文件
加载更多回复(13)

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧