0

I want to write or insert stored procedure into a SQL Server database from a console application in C#. How can I do this?

I have a stored procedure in a SQL script file and it's in one folder in my console application folder. Now how can I insert or write that stored procedure in SQL Server when my console app is running?

Regards

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vinit Patel
  • 2,408
  • 5
  • 28
  • 53
  • Open the script file, read it, open the database connection, create a command with the script data, execute the command. And hope that none has tampered with your script – Steve Mar 03 '14 at 10:25
  • You should really rewrite your question. It is very unclear what you are trying to achieve. – Luke Marlin Mar 03 '14 at 10:25
  • Have a look in the below link, http://stackoverflow.com/questions/650098/how-to-execute-an-sql-script-file-using-c-sharp – BAdmin Mar 03 '14 at 10:26
  • Thank you, you helped me –  Jun 10 '15 at 10:54

2 Answers2

2

Take a look Here and Here. Those links might help you to get through it.

Community
  • 1
  • 1
spaghettifunk
  • 1,936
  • 4
  • 24
  • 46
1

You execute an action statement against a database by calling ExecuteNonQuery on a SqlCommand. If you want to actually execute the script file rather than just the SQL code it contains then you can use Process.Start to run SQLCMD. I'm not sure of the exact commandline but you can look that up.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46