I have a C# console app ‘App1’ that reads a row of data from a table in a SQL Server 2005 DB. I want App1 to pass all the data in this row to App2, another C# console app. What is the best way to do this?
My first (naive) attempt was to do this:
object[] o = myrow.ItemArray; // make a string that separates each item by a space... for example '1 2 myVar'. // pass this string to App2 via command line.
This has some flaws: what if one of the entries in the row was ‘my var’ instead of ‘myVar’? Also, the order of the items would be hardcoded in the receiving app (App2).
So what’s the best way to do this? Would it be appropriate to pass an xml string to App2 via command line?
Cheers!
One approach would be to serialize the row to XML and use that, except that DataRow (lacking a default constructor) can’t be serialized. Instead you’d have to create a new DataTable and add that row to it.
Then you could simply serialize the entire DataTable to XML and pass it to the other application, either as a command-line argument or by saving the XML to a file and passing the filename.
Serializing a DataTable to XML is quite trivial thanks to the DataTable.WriteXml method.