Wednesday, September 13, 2006

Call SQLCMD from .Net with Output Capture

Stark's Blog - Execute sqlcmd (oSql) from .NET and capture the output

"I have recently seen quite a few questions about executing sqlcmd (oSql for those on 2000) from .NET code.  I have a bunch of utilities that I have written that do this and thought I would share the basic code to do it.

You can also easily capture the output as a stream and do whatever you want with it. In the example below I dump it to a file....I also use the -o to output the results of the sql that was excuted.

Just use the RedirectStandardOutput and RedirectStandardError ..."

Nice.

I was asked just last week if I knew of a way to execute SQL scripts from within .Net2 against SQL2k5 DB's.

I think this might just be the ticket...

2 comments:

Anonymous said...

Did you ever try this? I had problems, probably because I was getting the results back in a separate thread.

Greg said...

No, I haven't yet. I'll probably go with the DMO/SMO method I posted here instead...

It seems a little cleaner than the OSQL method...