데이터 대량 추가 SqlBulkCopy
13067 단어 copy
1 using System.Data.SqlClient;
2
3 class Program
4 {
5 static void Main()
6 {
7 string connectionString = GetConnectionString();
8 // Open a sourceConnection to the AdventureWorks database.
9 using (SqlConnection sourceConnection =
10 new SqlConnection(connectionString))
11 {
12 sourceConnection.Open();
13
14 // Perform an initial count on the destination table.
15 SqlCommand commandRowCount = new SqlCommand(
16 "SELECT COUNT(*) FROM " +
17 "dbo.BulkCopyDemoMatchingColumns;",
18 sourceConnection);
19 long countStart = System.Convert.ToInt32(
20 commandRowCount.ExecuteScalar());
21 Console.WriteLine("Starting row count = {0}", countStart);
22
23 // Get data from the source table as a SqlDataReader.
24 SqlCommand commandSourceData = new SqlCommand(
25 "SELECT ProductID, Name, " +
26 "ProductNumber " +
27 "FROM Production.Product;", sourceConnection);
28 SqlDataReader reader =
29 commandSourceData.ExecuteReader();
30
31 // Open the destination connection. In the real world you would
32 // not use SqlBulkCopy to move data from one table to the other
33 // in the same database. This is for demonstration purposes only.
34 using (SqlConnection destinationConnection =
35 new SqlConnection(connectionString))
36 {
37 destinationConnection.Open();
38
39 // Set up the bulk copy object.
40 // Note that the column positions in the source
41 // data reader match the column positions in
42 // the destination table so there is no need to
43 // map columns.
44 using (SqlBulkCopy bulkCopy =
45 new SqlBulkCopy(destinationConnection))
46 {
47 bulkCopy.DestinationTableName =
48 "dbo.BulkCopyDemoMatchingColumns";
49
50 try
51 {
52 // Write from the source to the destination.
53 bulkCopy.WriteToServer(reader);
54 }
55 catch (Exception ex)
56 {
57 Console.WriteLine(ex.Message);
58 }
59 finally
60 {
61 // Close the SqlDataReader. The SqlBulkCopy
62 // object is automatically closed at the end
63 // of the using block.
64 reader.Close();
65 }
66 }
67
68 // Perform a final count on the destination
69 // table to see how many rows were added.
70 long countEnd = System.Convert.ToInt32(
71 commandRowCount.ExecuteScalar());
72 Console.WriteLine("Ending row count = {0}", countEnd);
73 Console.WriteLine("{0} rows were added.", countEnd - countStart);
74 Console.WriteLine("Press Enter to finish.");
75 Console.ReadLine();
76 }
77 }
78 }
79
80 private static string GetConnectionString()
81 // To avoid storing the sourceConnection string in your code,
82 // you can retrieve it from a configuration file.
83 {
84 return "Data Source=(local); " +
85 " Integrated Security=true;" +
86 "Initial Catalog=AdventureWorks;";
87 }
88 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
파일 내용 및 파일 경로의 단수 및 복수 대체 텍스트를 사용하여 원본 파일을 대상에 붙여넣기기본 코드로 많은 수의 파일과 폴더를 복사하고 파일 내부의 여러 줄과 파일 및 폴더의 이름을 바꿔야 하는 경우가 많으며 시간이 많이 걸립니다😢. 이 문제를 해결하기 위해 나를 위해 할 수 있는 유틸리티를 작성했습니다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.