2

我有一个 .NET 项目,它正在接收数据、对其进行处理,然后将其写入 SQLServer 数据库。假设我有以下代码。

Private Sub InsertRecord(val1 As Integer, val2 As Integer, ByRef dbConnection As      Data.SqlClient.SqlConnection)
    Dim cmd As SqlCommand = Nothing
    If dbConnection.State = ConnectionState.Open Then
        Dim strSql As String = "Insert Into MyTable(Value1, Value2) Values(@Value1, @Value2)"
        cmd = New SqlCommand(strSql, dbConnection)
        cmd.Parameters.AddWithValue("@Value1", val1)
        cmd.Parameters.AddWithValue("@Value2", val2)
        Dim returnVal = cmd.ExecuteNonQuery()
        If Not returnVal Then
            'do other stuff
        End If
        cmd.Dispose()
    End If
End Sub

有时我需要快速插入记录——比如每秒 500 条。照原样,它每秒只能处理大约 200 次插入。有没有办法组合多个命令一次处理?我需要知道每个插入语句的结果。

4

1 回答 1

0

You need SQL Server bulk insert as made available through the SqlBulkCopy class. Use one transaction for all rows. It will be blindingly fast.

Indexing also plays into this, as well as many other things. Not enough information here to comment specifically.

于 2013-09-24T18:34:56.507 回答