Npgsql BeginTextImport 尝试从文件导入(而不是从 STDIN)
Npgsql BeginTextImport try import from file (not from STDIN)
我尝试通过 npgsql BeginTextImport 从文件导入数据到 postgresql table
这是我的代码:
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;
}
当我 运行 出现错误时 - 查看屏幕:
有可能吗?
PostgreSQL 的 "COPY from a file" 功能并不像您认为的那样;它不会从 client 端(Npgsql 是 运行)上的文件导入数据,而是从 server[=16] 上的文件导入数据=] 端(其中 PostgreSQL 为 运行)。换句话说,你可以把一个文件放在你的 PostgreSQL 服务器上,然后告诉 PostgreSQL 导入它。
如果你想在客户端机器上导入一个文件,你需要在 C# 中打开它,从中读取并写入 BeginTextImport returns 的 TextWriter returns。
现在我的代码运行良好,(再次感谢@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;
}
我尝试通过 npgsql BeginTextImport 从文件导入数据到 postgresql table 这是我的代码:
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;
}
当我 运行 出现错误时 - 查看屏幕:
有可能吗?
PostgreSQL 的 "COPY from a file" 功能并不像您认为的那样;它不会从 client 端(Npgsql 是 运行)上的文件导入数据,而是从 server[=16] 上的文件导入数据=] 端(其中 PostgreSQL 为 运行)。换句话说,你可以把一个文件放在你的 PostgreSQL 服务器上,然后告诉 PostgreSQL 导入它。
如果你想在客户端机器上导入一个文件,你需要在 C# 中打开它,从中读取并写入 BeginTextImport returns 的 TextWriter returns。
现在我的代码运行良好,(再次感谢@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;
}