C# 将数据从SQL Server导出到PostgreSQL

C# 将数据从SQL Server导出到PostgreSQL,c#,sql-server,postgresql,export,C#,Sql Server,Postgresql,Export,我使用这个示例将数据从SQL Server导出到PostgreSQL,当我开始导出时,300000行需要12分钟,我可以做些什么来加快这个过程,或者您知道另一种方法吗 string SourceDriver = "Driver={SQL Server Native Client 10.0}"; OdbcConnection SourceConnection = new OdbcConnection(SourceDriver+ ";Server=10.10.10.10;Database=sourc

我使用这个示例将数据从SQL Server导出到PostgreSQL,当我开始导出时,300000行需要12分钟,我可以做些什么来加快这个过程,或者您知道另一种方法吗

string SourceDriver = "Driver={SQL Server Native Client 10.0}";
OdbcConnection SourceConnection = new OdbcConnection(SourceDriver+ ";Server=10.10.10.10;Database=sourceMSSQL;Uid=sa;Pwd=12345;");

string DestDriver = "Driver={PostgreSQL}";
OdbcConnection DestConnection = new OdbcConnection(DestDriver+ ";Server=10.10.10.11;Port=5432;Database=destPostgreSQL;Uid=postgres;Pwd=12345;");

string SourceSql = "SELECT Code, Label, Model, List, Size, Quantity, City, Family,  ExportDate FROM MovPedidosP0";
string DestSql = "INSERT INTO tmp_MovPedidosP0_t (Code, Label, Model, List, Size, Quantity, City, Family,  ExportDate) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";

using(OdbcCommand SourceCommand = new OdbcCommand(SourceSql, SourceConnection))
{
    SourceConnection.Open();
    using(OdbcDataReader SourceReader = SourceCommand.ExecuteReader())
    {
        Console.WriteLine("Exporting...");

        DestConnection.Open();

        while(SourceReader.Read())
        {
            using(OdbcCommand DestCommand = new OdbcCommand(DestSql, DestConnection))
            {
                DestCommand.Prepare();
                DestCommand.Parameters.Clear();

                for(int i=0; i<SourceReader.FieldCount; i++)
                {
                    DestCommand.Parameters.AddWithValue("?ID" + (i+1).ToString(), SourceReader[i]);
                }

                DestCommand.ExecuteNonQuery();
                TotalRows++;
            }
        }

        DestConnection.Close();
    }
}

SourceConnection.Close();
string SourceDriver=“Driver={SQL Server本机客户端10.0}”;
OdbcConnection SourceConnection=新的OdbcConnection(SourceDriver+“服务器=10.10.10.10;数据库=sourceMSSQL;Uid=sa;Pwd=12345;”;
字符串DestDriver=“Driver={PostgreSQL}”;
OdbcConnection DestConnection=新的OdbcConnection(DestDriver+”;服务器=10.10.10.11;端口=5432;数据库=destPostgreSQL;Uid=postgres;Pwd=12345;”;
string SourceSql=“从MOVP中选择代码、标签、型号、列表、大小、数量、城市、族、导出日期”;
string DestSql=“插入tmp_movpidedosp0_t(代码、标签、型号、列表、大小、数量、城市、系列、出口日期)值(?,,,,,,,,,,,,?)”;
使用(OdbcCommand SourceCommand=newodbccommand(SourceSql,SourceConnection))
{
SourceConnection.Open();
使用(OdbcDataReader SourceReader=SourceCommand.ExecuteReader())
{
Console.WriteLine(“导出…”);
DestConnection.Open();
while(SourceReader.Read())
{
使用(OdbcCommand DestCommand=newodbccommand(DestSql,DestConnection))
{
DestCommand.Prepare();
DestCommand.Parameters.Clear();

对于(int i=0;i尝试使用本机NpgsqlConnection和SqlConnection,而不是Odbc连接


如果您使用SSIS导出到文本文件并使用命令导入,则可能会更简单、更快。

您可以查看PostgreSql的批处理:甚至可以使用SSIS直接复制到PostgresSQL数据库。我会使用SSIS和copy,但我需要通过应用程序导出信息,并且是将使用它的最终用户.