SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to execute sql script from C#


How to execute sql script from C#

Author
Message
niteshrajgopal
niteshrajgopal
SSC-Enthusiastic
SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)SSC-Enthusiastic (141 reputation)

Group: General Forum Members
Points: 141 Visits: 336
Hi Guys,

I have this code to execute a sql script in C#. It works great but I'm not happy with the dependency on the following. If the PC I want to run this on has no SQL Server installed this will fail. I need a better way. It needs to work for scripts that have GO statements in them as well.

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;


if (File.Exists(filePath))
{
FileInfo file = new FileInfo(filePath);
string script = file.OpenText().ReadToEnd();

using (SqlConnection conn = new SqlConnection(connectionString))
{

Server server = new Server(new ServerConnection(conn));
ReturnValue = server.ConnectionContext.ExecuteNonQuery(script);
}

file.OpenText().Close();
}



Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27281 Visits: 13268
Do you really need to install the entire SQL Server? I think the SQL client alone is probably enough.

The GO statement is not a T-SQL statement, but a batch seperator for SSMS and SQLCMD.
It will not work outside those environments.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28103 Visits: 39938
you can explicitly extract both those DLLs from the GAC(Global Assembly Cache), and then make sure they are in your bin folder with the executable you are calling.
in that way, your references are available, without having to install SQL on the machine it is executed from.

the problem, of course, is you need someone to select the target server with the appropriate permissions., and split the document up into multiple separate commands based on the GO statement, and execute each of those commands in a loop.

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search