Postgre COPY BeginBinaryExport While loop - c#

I'm using C# Npgsql and am trying to export data from Postgres using the COPY command. The below query will return 100+ rows. I would like to read the results out using a while loop just like you would do with NpgsqlDataReader.
The documentation on http://www.npgsql.org/doc/copy.html keeps throwing errors when I try to get a second row of results (second StartRow()).
Notes: Running second StartRow() returns following error message below:
An unhandled exception of type 'Npgsql.NpgsqlException' occurred in mscorlib.dll
Additional information: Unknown message code: 0
using (NpgsqlBinaryExporter reader = conn.BeginBinaryExport("COPY tableName(colONE, colTWO) TO STDOUT (FORMAT BINARY)"))
{
reader.StartRow();
Console.WriteLine(reader.Read<string>());
Console.WriteLine(reader.Read<int>(NpgsqlDbType.Smallint));
reader.StartRow(); //### <== ERROR HERE ###
Console.WriteLine(reader.Read<string>());
Console.WriteLine(reader.Read<int>(NpgsqlDbType.Smallint));
}
How do I read out ALL the rows in a WHILE loop or output in one shot?

If I understand the question correctly something like this might help:
using (NpgsqlBinaryExporter reader = conn.BeginBinaryExport("COPY tableName(colONE, colTWO) TO STDOUT (FORMAT BINARY)"))
{
while(reader.StartRow() > 0)
{
Console.WriteLine(reader.Read<string>());
Console.WriteLine(reader.Read<int>(NpgsqlDbType.Smallint));
}
}
This should read all the data from the table, and it solved my similar issue. I am still looking at ways of improving the performance, so let me know if you have any ideas.

Related

IMAPI: COMException Internal file system error occurred [-1062555360 ]

I am writing CD/DVD using IMAPI with C#.NET windows application.
The data I write on CD contains one executable file (test.exe) which is also developed with C#.NET and virtualized (sandobx) using Turbo Virtualization Studio.
All data to be written on CD is placed in one folder (source path) on C drive.
Following is small code snippet: -
IStream stream = null;
try
{
System.Diagnostics.Debug.Print("Adding - " + thisFileItem.SourcePath);
if (thisFileItem.SourcePath != null)
Win32.SHCreateStreamOnFile(thisFileItem.SourcePath, Win32.STGM_READ | Win32.STGM_SHARE_DENY_WRITE, ref stream);
if (stream != null)
{
fileSystemImage.Root.AddFile(thisFileItem.DestPath + thisFileItem.DisplayName, stream);
}
}
finally
{
if (stream != null)
{
Marshal.FinalReleaseComObject(stream);
}
}
Call to "fileSystemImage.Root.AddFile" method while adding test.exe throws COMException -1062555360 "Internal file system error occurred." All other files add and write properly.
Exception Details: -
COMException -1062555360
Internal file system error occurred.
at ImapiInterop.IFsiDirectoryItem.AddFile(String path, IStream fileData)
at ImapiImplementation.CDWriter.objBackgroundWorker_DoWork(Object sender, DoWorkEventArgs e) in C:\.........\CDWriter.cs:line 405
If I put my source folder on some other location (Desktop or D drive), all writing process (including test.exe) happens fine without error.
I suspect the issue is due to virutalization but not sure.
Please help.
The error message returned by IMAPI is incorrect and that is why all confusion.
Refer following link.
social.msdn.microsoft.com
Following is the text copied from answer (from Dmitri) on above site: -
IMAPI supports the ISupportErrorInfo interface, and we are aware of
the issue of mismatching error messages in your scenario.
Internally, IMAPI creates rollback objects to undo add/remove file
actions. We've had an issue where the rollback action was created
prematurely, so after the return code for IFsiDirectoryItem::AddFile
was already set, the rollback action was to remove the file from the
image. Since the file hasn't been added, IMAPI_E_FSI_INTERNAL_ERROR
exception was thrown, which changed the IErrorInfo message to the one
you're seeing.
We are aware of this issue, and it will be fixed in the next release
of IMAPI. Unfortunately, it is not serious enough to be addressed in a
hotfix.

What's the cleanest way to get record as string in FileHelpers?

When I'm parsing a text file, and importing it in my application through the (truly wonderful) FileHelpers library, for each error that occurs I want to log in a separate text file the record that failed (along with the error of course), like
Error: The record ..... failed with error ....
This brings the question in the title: I see there's the method
WriteString(IEnumerable<T>), but if I call
string recordAsString = engine.WriteString(new List<T>() { record });
the result string contains a new line in it, and the only way I managed to "clean" it is
recordAsString = recordAsString.Substring(0, recordAsString.Length - 2);
Is there a cleaner way, ie a method that returns the record as a string that doesn't contain any new line?
Thank you
You can try with
var recordAsString = engine.Options.RecordToString(record);
Thanks for using the library

dotnetrdf xml exception using QueryWithResultSet

I have an asp.net project in which, I would like to query DBPedia.
Using the following code I am getting an error:
public string testEndpoint()
{
//TEST02
SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org");
string res = "";
//Make a SELECT query against the Endpoint
SparqlResultSet results = endpoint.QueryWithResultSet("SELECT ?year WHERE {dbpedia:Rihanna dbpedia-owl:birthYear ?year}");
foreach (SparqlResult result in results)
{
res = result.ToString();
Console.WriteLine(result.ToString());}
Error message: "An exception of type 'System.Xml.XmlException' occurred in dotNetRDF.dll but was not handled in user code".
Even if I handle the exception the method cannot be executed. Regarding the details it says that there is an invalid XML-version 1.1.
As the XML comes from DBPedia I don't know how to change the xml version or how else I can handle this problem.
Virtuoso, which is the triple store used behind the dbpedia SPARQL endpoint, has updated its XML result generation. They replaced the XML version 1.0 with 1.1 in this commit. This causes the exception in the dotNetRDF parser.
Later on Virtuoso reverted the changes in the XML header. Hopefully DBPedia will update their binaries soon, so the old XML header appears again in the SPARQL results.
Source: http://github.com/openlink/virtuoso-opensource/issues/405

Add to List<string> from text file

I have a List<string> that I would like to populate via a text file that is set as a project resource. I have looked all over on a way to do this but haven't yet found one that doesn't cause my program to crash.
If I manually populate the list...
_names.Add("Sam");
_names.Add("John");
_names.Add("Mike");
...everything works. My text file has each name on a separate line, no commas or anything. When I try to read in the names, the program crashes, no matter which route I take. This is the most recent way I've tried, though there are many others:
using (var reader = new StreamReader(Properties.Resources.sampleNamesMale))
{
string line;
while ((line = reader.ReadLine()) != null)
{
_names.Add(line);
}
}
Also, I can't isolate the reason for the crash because every time it does, the error just mentions ViewModelLocator, which is entirely irrelevant to this issue.
Does anybody have any ideas about how to fix this? I would certainly appreciate any advice.
Update: Try-catch yields no results. This is the error I get:
XamlParseException occurred - 'The invocation of the constructor on type 'AoW.ViewModels.ViewModelLocator' that matches the specified binding constraints threw an exception.' Line number '13' and line position '10'.
It points at InitializeComponent() in my main window's constructor.
Update 2: The real exception is this:
"ArgumentException occurred - Illegal characters in path." It points at the using (var reader.... line.
Use a StringReader instead of a StreamReader:
using (var reader = new StringReader(Properties.Resources.sampleNamesMale))
You are getting that error because StreamReader(string) expects a file path. If you are providing the actual text in Properties.Resources.sampleNamesMale, you have to use a StringReader.
The only way you could get the exception:
ArgumentException occurred - Illegal characters in path.
is if the path returned by Properties.Resources.sampleNamesMale was literally invalid:
using (var reader = new StreamReader(Properties.Resources.sampleNamesMale))
After second update the answer is very easy: display in debugger what is a path to your file and make it correct. Probably it contains spaces in the end or not escaped \

IDataRecord.IsDBNull causes an System.OverflowException (Arithmetic Overflow)

I have a OdbcDataReader that gets data from a database and returns a set of records.
The code that executes the query looks as follows:
OdbcDataReader reader = command.ExecuteReader();
while (reader.Read())
{
yield return reader.AsMovexProduct();
}
The method returns an IEnumerable of a custom type (MovexProduct). The convertion from an IDataRecord to my custom type MovexProduct happens in an extension-method that looks like this (abbrev.):
public static MovexProduct AsMovexProduct(this IDataRecord record)
{
var movexProduct = new MovexProduct
{
ItemNumber = record.GetString(0).Trim(),
Name = record.GetString(1).Trim(),
Category = record.GetString(2).Trim(),
ItemType = record.GetString(3).Trim()
};
if (!record.IsDBNull(4))
movexProduct.Status1 = int.Parse(record.GetString(4).Trim());
// Additional properties with IsDBNull checks follow here.
return movexProduct;
}
As soon as I hit the if (!record.IsDBNull(4)) I get an OverflowException with the exception message "Arithmetic operation resulted in an overflow."
StackTrace:
System.OverflowException was unhandled by user code
Message=Arithmetic operation resulted in an overflow.
Source=System.Data
StackTrace:
at System.Data.Odbc.OdbcDataReader.GetSqlType(Int32 i)
at System.Data.Odbc.OdbcDataReader.GetValue(Int32 i)
at System.Data.Odbc.OdbcDataReader.IsDBNull(Int32 i)
at JulaAil.DataService.Movex.Data.ExtensionMethods.AsMovexProduct(IDataRecord record) [...]
I've never encountered this problem before and I cannot figure out why I get it. I have verified that the record exists and that it contains data and that the indexes I provide are correct. I should also mention that I get the same exception if I change the if-statemnt to this: if (record.GetString(4) != null). What does work is encapsulating the property-assignment in a try {} catch (NullReferenceException) {} block - but that can lead to performance-loss (can it not?).
I am running the x64 version of Visual Studio and I'm using a 64-bit odbc driver.
Has anyone else come across this? Any suggestions as to how I could solve / get around this issue?
Many thanks!
For any one experiencing the same issue, the way I solved this was to switch from the Odbc* classes to their OleDb* counterparts. This of course demands that your data driver has support for OleDb connections.
Which DB are you trying to talk to? If it uses some "private" column types that can cause problems. That doesn't apply to SQL Server of course :-)
Also check that you are compiling and running as x64 (Process Explorer will show you thsi and even plain tack manager shows it). devenv.exe will still be x86 but your actual binary should run as x64. The reason I mention is that crossing 32/64 bit boundary is notorious for breaking 3rd party ODBC drivers.

Categories

Resources