从SQL进行批量插入获取数据时发生内存异常

时间:2018-11-23 12:49:22

标签: sql sql-server vb.net

我只是试图批量插入结果为system.out.memory的数据,即使Ram为260 GB和SQL Standard Edition 2017,也会引发异常。记录大约为100万,请使用下面的代码给我任何解决方案< / p>

 DataTable table = new DataTable();
            //  read the table structure from the database
            SqlDataAdapter adapter = new SqlDataAdapter("select * from xxxxx", constr);
            adapter.SelectCommand.CommandTimeout = 0;
            adapter.Fill(table);
            Console.WriteLine();
            Console.WriteLine(table.Rows.Count);
            Console.WriteLine(" Table End time..");
            Console.WriteLine(DateTime.Now);
            Console.WriteLine("end----)";
            Console.WriteLine();
            SqlBulkCopy bulk = new SqlBulkCopy(constr2);
            bulk.DestinationTableName = "xxxxxx";
            bulk.WriteToServer(table);
            Console.WriteLine()

1 个答案:

答案 0 :(得分:0)

也许是这样吗?

Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Data

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click


        Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\your_path\Book1.xls;Extended Properties=Excel 8.0;")

        ExcelConnection.Open()

        Dim expr As String = "SELECT * FROM [Sheet1$]"
        Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection)
        Dim objDR As OleDbDataReader
        Dim SQLconn As New SqlConnection()
        Dim ConnString As String = "Data Source=DB_Name;Initial Catalog=Northwind.MDF;Trusted_Connection=True;"
        SQLconn.ConnectionString = ConnString
        SQLconn.Open()

        Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)

            bulkCopy.DestinationTableName = "tblTest"

            Try
                objDR = objCmdSelect.ExecuteReader
                bulkCopy.WriteToServer(objDR)
                ExcelConnection.Close()

                'objDR.Close()
                SQLconn.Close()
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Using

    End Sub

End Class