从 Visual Studio 到 SQL 的数据上传服务器超时

Data upload from Visual Studio to SQL Server Timeout

我遇到了一个大问题,它通过 vb.net 将 SQL-Server Management Studio 制作的 SQL-Dump 发送到另一个 SQL-Server。 我制作了这样的 SQL-文件:数据库/任务/脚本...

删除转储中的每个 "GO" 命令后,我设法上传到我的本地 SQL-Server 2012。但这只有在我为任何 table 制作不同的脚本时才有效。 如果我使用一个大文件,我会超时。

现在我想通过 Internet 连接将数据 (50 MB) 传输到另一个 SQL 服务器。 此服务器上仅生成了 5 个表中的 3 个 :-(

我收到以下超时错误(德语版,抱歉):

"Eine Ausnahme (erste Chance) des Typs "System.Data.SqlClient.SqlException" 在 System.Data.dll 中。 超时 abgelaufen。 Das Zeitlimit wurde vor dem Beenden des Vorgangs überschritten oder der Server reagiert nicht。

知道哪一方超时(SQL-服务器顺序 vb.net)吗? 有没有更安全的方法将大量数据上传到 SQL-服务器?

这是我使用的代码(SQL-Skript 在My.Resources.SQLDump):

    '####SQLDump  #######
    Dim SQLscript As String = My.Resources.SQLDump


    SQLscript = "USE [" + TextBoxDB.Text + "] " + vbCrLf + SQLscript


    Using connection As New SqlConnection(Verbind.ConStr)

        Using command As New SqlCommand()
            ' Set the connection
            command.Connection = connection

            ' Not necessary, but good practice
            command.CommandType = CommandType.Text

            ' Example query using parameters
            command.CommandText = SQLscript

            Try
                connection.Open()
                rowsAffected = command.ExecuteNonQuery()
            Catch ex As Exception

                Debug.Print(ex.Message)
            Finally
                command.Dispose()
                connection.Dispose()
            End Try
            'command.ExecuteNonQuery()
        End Using ' Dispose Command

    End Using ' Dispose (and hence Close) Connection

    MsgBox("DB-Upload 1/5 fertig!")

Is there a safer method uploading lots of data to a SQL-Server?

是的,有几种方法可以将数据加载到 SQL 服务器

  1. bcp Utility
  2. BULK INSERT statement
  3. OPENROWSET
  4. SQL Server Import and Export Wizard

Important note: On a 64-bit computer, Integration Services installs the 64-bit version of the SQL Server Import and Export Wizard (DTSWizard.exe). However, some data sources, such as Access or Excel, only have a 32-bit provider available. To work with these data sources, you might have to install and run the 32-bit version of the wizard. To install the 32-bit version of the wizard, select either Client Tools or SQL Server Data Tools (SSDT) during setup.

  1. SSIS

    Tutorial 创建 SSIS 包

作为一名 SSIS 开发人员,我可以深入并演示使用 SSIS 导入数据的可能方法,例如

  • 批量导入
  • 脚本任务
  • 平面文件源/Excel源
  • 等等等等。但这与您提出的问题不同。

关于解决您的错误,您可以在代码中设置 command timeout 属性 使其 运行 具有任何特定时间限制或无限。

command.CommandTimeout = 1;

Note: The CommandTimeout property will be ignored during asynchronous method calls such as BeginExecuteReader.

已编辑 这是示例代码

Using command As New SqlCommand()
        command.Connection = connection
        command.CommandType = CommandType.Text
        command.CommandText = SQLscript
        command.CommandTimeout = 1
        Try
            connection.Open()
            rowsAffected = command.ExecuteNonQuery()
        Catch ex As Exception
            Debug.Print(ex.Message)
        Finally
            command.Dispose()
            connection.Dispose()
        End Try
End Using 

其他方法是扩展 connection timeout in your connection string。它接受表示秒数的整数(在下面的示例中,连接超时设置为 30 秒;0 表示无限制)

Connection Timeout=30;

一次性导入/导出向导

由于您能够生成脚本,因此您可以访问源代码。因为您能够执行它们,所以您可以访问目标,并且您安装了 Management Studio。 IMO,您最快的选择是 Sql 服务器提供的导入导出实用程序。

在 Sql Server Management Studio 中,右键单击源数据库名称,单击 "Tasks",然后单击 "Export Data"。这会打开一个向导,让您输入源、目标并选择要导出的对象。

虽然 50 MB 的脚本很大,但 50MB 的数据移动应该会在几分钟内完成。

一遍又一遍地移动数据

您有多种选择,我会选择 OpenRowset,尤其是考虑到您要移动的数据量。这是一个参考 https://msdn.microsoft.com/en-us/library/ms190312.aspx

如果您要定期移动价值 GB 的数据,那么 SSIS 包是您的最佳选择。这里有教程https://msdn.microsoft.com/en-us/library/ms169917.aspx