SQL 大数据导入

 
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = GetConnectionString();
        // Open a sourceConnection to the AdventureWorks database.
        using (SqlConnection sourceConnection =
                   new SqlConnection(connectionString))
        {
            sourceConnection.Open();

            // Perform an initial count on the destination table.
            SqlCommand commandRowCount = new SqlCommand(
                "SELECT COUNT(*) FROM " +
                "dbo.BulkCopyDemoMatchingColumns;",
                sourceConnection);
            long countStart = System.Convert.ToInt32(
                commandRowCount.ExecuteScalar());
            Console.WriteLine("Starting row count = {0}", countStart);

            // Get data from the source table as a SqlDataReader.
            SqlCommand commandSourceData = new SqlCommand(
                "SELECT ProductID, Name, " +
                "ProductNumber " +
                "FROM Production.Product;", sourceConnection);
            SqlDataReader reader =
                commandSourceData.ExecuteReader();

            // Open the destination connection. In the real world you would 
            // not use SqlBulkCopy to move data from one table to the other 
            // in the same database. This is for demonstration purposes only.
            using (SqlConnection destinationConnection =
                       new SqlConnection(connectionString))
            {
                destinationConnection.Open();

                // Set up the bulk copy object. 
                // Note that the column positions in the source
                // data reader match the column positions in 
                // the destination table so there is no need to
                // map columns.
                using (SqlBulkCopy bulkCopy =
                           new SqlBulkCopy(destinationConnection))
                {
                    bulkCopy.DestinationTableName =
                        "dbo.BulkCopyDemoMatchingColumns";

                    try
                    {
                        // Write from the source to the destination.
                        bulkCopy.WriteToServer(reader);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        // Close the SqlDataReader. The SqlBulkCopy
                        // object is automatically closed at the end
                        // of the using block.
                        reader.Close();
                    }
                }

                // Perform a final count on the destination 
                // table to see how many rows were added.
                long countEnd = System.Convert.ToInt32(
                    commandRowCount.ExecuteScalar());
                Console.WriteLine("Ending row count = {0}", countEnd);
                Console.WriteLine("{0} rows were added.", countEnd - countStart);
                Console.WriteLine("Press Enter to finish.");
                Console.ReadLine();
            }
        }
    }

    private static string GetConnectionString()
        // To avoid storing the sourceConnection string in your code, 
        // you can retrieve it from a configuration file. 
    {
        return "Data Source=(local); " +
            " Integrated Security=true;" +
            "Initial Catalog=AdventureWorks;";
    }
}
### 使用 phpMyAdmin 进行大数据量的数据导入 #### 修改配置文件 `config.inc.php` 为了能够顺利通过 phpMyAdmin 导入大型 SQL 文件,可以调整 phpMyAdmin 的配置文件来支持更大的文件上传。具体操作是在 `config.inc.php` 中找到 `$cfg['UploadDir']` 参数并设置其值为指定的目录名称,例如: ```php $cfg['UploadDir'] = 'ImportSQLFile'; ``` 这一步骤允许用户将要导入的大文件放置于该路径下,从而绕过常规浏览器上传方式所带来的大小限制问题[^5]。 #### 设置 PHP 和 Web 服务器参数 除了上述措施外,还需要适当放宽 PHP 及 web server 对单次请求处理时间和内存占用等方面的约束条件。对于 Apache 或 Nginx 用户来说,在对应的虚拟主机配置里增加如下几项指令会有所帮助: - **PHP 配置** 编辑 `/etc/php/7.x/apache2/php.ini` (根据实际版本号),修改或添加以下条目: ```ini post_max_size=200M upload_max_filesize=200M max_execution_time=300 memory_limit=256M ``` - **Web Server 配置** 如果使用的是 Apache,则可以在 `.htaccess` 文件中加入超时时间控制;如果是 Nginx 则需编辑站点配置中的 client_body_timeout 值以延长客户端连接等待时限。 完成这些更改之后记得重启相应的服务使新设置生效[^1]。 #### 替代方案:命令行工具与第三方软件 考虑到直接利用 phpMyAdmin 完成大规模数据迁移可能存在风险以及效率低下等问题,建议考虑采用更专业的手段如 MySQL 自带命令行工具或是像 Navicat For MySQL 这样的图形界面应用程序来进行这项工作。特别是当面对非常庞大的数据库备份恢复需求时,后者往往能提供更好的性能表现和用户体验[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值