473,383 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

.Net Equivilant for sqlcmd or osql

I am writing an application that automates running sql scripts against my
database. Currently, I am using Process.Start("sqlcmd") with a number of
parameters to accomplish this, but I would like to have more control than
running an outside process. I am hoping that there is a .Net class that I
can call directly to run sql scripts, but so far, I have not had any luck
finding one. Can anyone give me any advice to accomplish this task?
Jul 23 '08 #1
5 3911
What is the database? Any of the DbCommand implementations (SqlCommand
etc) should be able to pass down your script as text. Depending on what
you are doing, there are also higher-level management objects, but these
tend to be vendor-specific.

Marc
Jul 23 '08 #2
No, executing a DBCommand does not allow sql batches. My files are either
create scripts for procedures or tables. When I use a DBCommand on a
statement with more than one command, I get an exception. Osql and sqlcmd
allow execution of batches containing mulitple statements.

"Marc Gravell" wrote:
What is the database? Any of the DbCommand implementations (SqlCommand
etc) should be able to pass down your script as text. Depending on what
you are doing, there are also higher-level management objects, but these
tend to be vendor-specific.

Marc
Jul 23 '08 #3
For mysql, i had to do it by reading through the script one line at a time
(upto the ; at end of a sql line) calling executenonquery for each one.
Not sure about other database engines.

Jul 23 '08 #4
"Harry Keck" <Ha*******@discussions.microsoft.comwrote in message
news:0C**********************************@microsof t.com...
No, executing a DBCommand does not allow sql batches. My files are either
create scripts for procedures or tables. When I use a DBCommand on a
statement with more than one command, I get an exception. Osql and sqlcmd
allow execution of batches containing mulitple statements.
There's a trick: Read your script file and split it on the GO statements.
Each piece between two "GO"s can be sent to the server by means of an
ExecuteNonQuery. Besides splitting on the GOs, I believe that you also have
to strip the comments, which you can do by reading the file line by line and
removing everything from "--" to the end of each line.

Jul 23 '08 #5
I second this. Parsing on the semicolon is not the way that
applications like SQL Management Studio work.

Also, you do not have to strip the comments, it works just fine,
assuming you have carriage returns and the like.

Basically, attach a StreamReader to the FileStream that is opened to the
script. Read EACH line into a string buffer. If the line contains GO and
ONLY GO, then send everything that you have in the buffer (as you add each
new line, remember to add the newline back to the buffer). Then, clear the
buffer, and continue reading.

We've done the exact same thing where I work to take the scripts that
you would use in SQL Management Studio and execute them without running it.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:%2****************@TK2MSFTNGP03.phx.gbl...
"Harry Keck" <Ha*******@discussions.microsoft.comwrote in message
news:0C**********************************@microsof t.com...
>No, executing a DBCommand does not allow sql batches. My files are
either
create scripts for procedures or tables. When I use a DBCommand on a
statement with more than one command, I get an exception. Osql and
sqlcmd
allow execution of batches containing mulitple statements.

There's a trick: Read your script file and split it on the GO
statements. Each piece between two "GO"s can be sent to the server by
means of an ExecuteNonQuery. Besides splitting on the GOs, I believe that
you also have to strip the comments, which you can do by reading the file
line by line and removing everything from "--" to the end of each line.

Jul 23 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bob | last post by:
Everybody, I've been doing a lot of on-line research and cannot find any reference to the exact problem I'm having. Let me preface this question with the fact that I'm coming from an Oracle...
2
by: Murtix Van Basten | last post by:
Hi, I have dumped a very large database from mysql (using mysqldump program) as a raw sql file. The reason was, convert this database to a MSSQL database. Since mysqldump creates the file as raw...
24
by: LineVoltageHalogen | last post by:
Greetings All, I was hoping that someone out there has run into this issue before and can shed some light on it for me. I have a stored procedure that essentially does a mini ETL from a source...
2
by: Jeff_in_MD | last post by:
Hi, I'm trying to add a column to a table, then update that column with a query. This is all within a single batch. Sqlcmd gives me an error on the update, saying "invalid column xxx", because...
11
by: gopal | last post by:
Hi, I am trying to call the OSQL utility from my C# console application and i am having problems i have the following code In Main method ProcessStartInfo psi = new...
7
by: gopal | last post by:
Hi, I have a console application whose purpose is to run the OSQL utility from my console application. When my application is run, the OSQL utility is started and it has to prompt for...
0
by: KonRi | last post by:
I don't know how to pass strange looking passwords to sqlcmd. In sql I use 'pass' and that works fine, but what to do with command line applications like sqlcmd? if password contains no " sumbol I...
2
by: Desmodromic | last post by:
The command below runs fine from within Management Studio exec sp_MSforeachtable @command1="exec sp_spaceused '?' " However, I'd like to run it via sqlcmd. I've tried various combinations of...
7
by: bobdurie | last post by:
I'm trying to run an import of some data that has unix style carriage returns throughout (ie, 0x0A, or \n). When i use osql to import the data, it shows up in the SQL Server 2005 database as 0x0D...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.