简体   繁体   English

如何在c#中实施此计划以读取所有数据并写入另一个表?

[英]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中有两个表:

  • Table 1 with columns name , family , address 表1的namefamilyaddress
  • Table 2 with columns name , family 表2的列namefamily

And my server properties is this: 我的服务器属性是这样的:

  • RAM: 64GB 内存:64GB
  • Intel 24 core xeon 英特尔24核心至强

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.

相关问题 如何读取用C#或Java发送到另一个进程的所有网络数据? - How can I read all the network data sent to another process in C# or Java? 如何使用我的计划在C#中实现网站文本框自动检测? - How can i implement the website textbox auto detect in c# with my plan? C#中的套接字,我如何通过NetworkStream异步读取和写入数据 - Sockets in C#, how can I asynchronously read and write data through a NetworkStream 如何从C#中的另一个表读取数据 - How read data from the another table in c# 如何在C#中实现对另一台服务器的回调 - How can I implement callbacks to another server in c# 如何从C#的数据表中的给定HTML文档中读取特定数据? - How can I read specific data from given html document in Data Table in c#? 如何写入C#中另一个进程正在使用的文件? - How can I write to a file that is in use by another process in C#? 在继续之前,如何从serialPort读取所有数据? C# - How can I read all data from a serialPort before continuing? c# C#-将数据写入流+与另一个流读取数据 - C# - Write Data To A Stream + Read Data With Another Stream C#/ WPF如何从文本框读取“绑定”(数据从SQL表获取)? - C#/WPF How can I read “Bindings” from a TextBox (Data gets from a SQL-Table)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM