3
Answers

Executing SQL scripts from C#

evanwang

evanwang

20y
2k
1
Is there any programmatic way to execute a sql script from C#? I'm using SQL Server and I do not want to use osql.exe. Here is an example: CREATE TABLE... GO CREATE TABLE... GO CREATE CONSTRAINT... GO INSERT DATA... GO I tried reading the file and setting the CommandText in the SqlCommand object, but since the GO needs to be in a separate line when executing, SQL Server gives an error. I tried some various newline characters, but none worked. Any thoughts? Thanks, Evan
Answers (3)
1
Sam Hobbs

Sam Hobbs

NA 28.7k 1.3m 15y

I don't know how you are doing it so I don't know if there is an easier way. My guess is that there is not an easier way.
 
Note that often the "secret" to making a program that is easy to use is complex development. In other words, sometimes we must write complicated program to make one that is easy to use. Windows and .Net and database software such as SQL Server does a lot for us, but sometimes we must do some programming outselves.
 
My guess is that the code necessary for the search page is not difficult except for many details that need to be done.
 
Probably what I would do is to create a List<String> for each filter (selection criteria) then concatenate them all together as needed.
0
Vikas Ahlawat

Vikas Ahlawat

NA 577 661.6k 15y
Ya code is not difficult.
Thanks for your reply.