tags:

views:

231

answers:

3

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!

+1  A: 

The space-separated approach is fine if you are using Process.Start - you just need to wrap items containing spaces with quotes - same as at the command line: cd "c:\program files"

If the data is more complex than a few values, then IPC approaches such as remoting, sockets, WCF, etc might help. Or simpler: write the data (perhaps as xml) to a file, and have the second app load the data from the file.

Marc Gravell
+3  A: 

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.

Matt Hamilton
+1  A: 

You could use a serialized DataSet to easily get the data from one place to another without having to write a lot of custom code, as the default DataSet already provides the necessary methods ( .WriteXML for example will serialize the DataSet to XML and write to a file). Your other application could then poll the appropriate directory for new files.

tbreffni