Npgsql BeginTextImport尝试从文件导入(而不是从STDIN导入)

时间:2015-11-09 15:49:04

标签: npgsql

我尝试通过npgsql BeginTextImport从文件导入数据到postgresql表 这是我的代码:

public Object Copy(String sSchemaAndTableName, String sFilePath, Boolean bIsImport)
    {
        Boolean bRet = true;

        Object oResult = new Object();
        NpgsqlConnection conn = new NpgsqlConnection(sConnectionString);
        NpgsqlCommand cmd = new NpgsqlCommand();            

        try
        {
            conn.Open();
            NpgsqlTransaction transaction = conn.BeginTransaction();

            if (File.Exists(sFilePath))
            {
                try
                {
                    if (bIsImport)
                    {
                        conn.BeginTextImport("COPY " + sSchemaAndTableName + " FROM '" + sFilePath + "';");
                    }
                    else
                    {
                        conn.BeginTextExport("COPY " + sSchemaAndTableName + " TO '" + sFilePath + "';");
                    }
                }
                catch (Exception e)
                {
                    bRet = false;
                    transaction.Rollback();

                    throw e;
                }
                finally
                {
                    if (bRet)
                    {
                        transaction.Commit();
                    }
                }
            }
            else
            {
                throw new Exception("Plik nie istnieje: " + sFilePath);
            }

        }
        catch (Exception ex)
        {                                
            MW.Core.Common.Objects.Exceptions.Items.Add(ex);
            oResult = null;
        }
        finally
        {
            cmd.Dispose();

            conn.Close();
            conn.Dispose();
        }

        return oResult;
    }

当我运行这个我得到错误 - 看看屏幕:

when i use myapp directory

when i use postresql server data directory - this works when i use pgadmin but from my app via npgsql not

有可能吗?

2 个答案:

答案 0 :(得分:2)

PostgreSQL""从文件中复制"功能不会做你认为它做的事情;它不会从客户端端(运行Npgsql)的文件导入数据,而是从服务器端的文件导入数据(PostgreSQL正在运行) )。换句话说,您可以将文件放在PostgreSQL服务器上并告诉PostgreSQL导入它。

如果要在客户端计算机上导入文件,则需要在C#中打开它,从中读取并写入BeginTextImport返回的TextWriter。

答案 1 :(得分:2)

现在我的代码很棒,(再次感谢@Shay Rojansky)

public Boolean CopyFrom(String sDestinationSchemaAndTableName, String sFromFilePath)
    {
        Boolean bRet = true;

        NpgsqlConnection conn = new NpgsqlConnection(sConnectionString);
        NpgsqlCommand cmd = new NpgsqlCommand();            

        try
        {
            conn.Open();
            NpgsqlTransaction transaction = conn.BeginTransaction();

            if (File.Exists(sFromFilePath))
            {
                try
                {
                    using (var writer = conn.BeginTextImport("COPY " + sDestinationSchemaAndTableName + " FROM STDIN"))
                    {
                        foreach (String sLine in File.ReadAllLines(sFromFilePath))
                        {
                            writer.WriteLine(sLine);
                        }
                    }
                }
                catch (Exception e)
                {
                    bRet = false;
                    transaction.Rollback();

                    throw e;
                }
                finally
                {
                    if (bRet)
                    {
                        transaction.Commit();
                    }

                    transaction.Dispose();
                }
            }
            else
            {
                MW.Core.Common.Objects.Exceptions.Items.Add(new Exception("Plik nie istnieje: " + sFromFilePath));
            }
        }
        catch (Exception ex)
        {                                
            MW.Core.Common.Objects.Exceptions.Items.Add(ex);
            bRet = false;
        }
        finally
        {
            cmd.Dispose();

            conn.Close();
            conn.Dispose();
        }

        return bRet;
    }