[英]How can I implement this plan in c# to read all data and write another table?
I have two tables in SQL Server: 我在SQL Server中有两个表:
name
, family
, address
name
, family
, address
列 name
, family
name
, family
And my server properties is this: 我的服务器属性是这样的:
Im the table 1 may be have 30 million, and I want read all data that, and just insert name and family from table 1 to table 2. I'm using Linq-to-SQL, but that is slow, how can I implement that scenario rapidly in C#? 我的表1可能有3000万,我想读取所有数据,只将表1的名称和家族插入表2。我使用的是Linq-to-SQL,但这很慢,我如何实现用C#迅速解决这种情况? Thanks.
谢谢。
You can execute the following query on the server: 您可以在服务器上执行以下查询:
INSERT INTO table2
SELECT Name, Family
FROM table1
That will copy all the records from table1
into table2
directly on the SQL Server. 这样会将所有记录从
table1
直接复制到SQL Server上的table2
。
Of course, if you must do it from C#, you can execute the same query using a SQLCommand. 当然,如果必须从C#执行此操作,则可以使用SQLCommand执行相同的查询。
using (SqlConnection connection = new SqlConnection(connectionString))
{
String query = @"INSERT INTO table2
SELECT Name, Family
FROM table1";
SqlCommand command = new SqlCommand(query, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
If you need to do some processing to the data in C# a good option is to use the SqlBulkCopy class but it's going to take a bit more work to set up the operation. 如果您需要对C#中的数据进行一些处理,一个不错的选择是使用SqlBulkCopy类,但是要设置该操作将需要更多的工作。
You can using following code: 您可以使用以下代码:
// db = DataContext
var toInsert = from t1 in db.Table1
where ...
select new Table2
{
name = b.name,
family = b.family
};
db.Table2.InsertAllOnSubmit(toInsert);
db.SubmitChanges();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.