Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I want to execute sql script in sql server through C#. here i want to get sql script path through app.config.

how can i do this

thanks
Posted
Comments
Sergey Alexandrovich Kryukov 8-Jan-12 2:07am    
OK, and what's "SQL script" and "SQL script path"?
--SA
K N R 8-Jan-12 2:27am    
hi sakryukov,
thanks for your reply,

In sql script have one stored procedure and the file is in ~/app_data/script.sql

Following first link will provide you the solution to get server and database information from web.config.
How to retrieve the databaseName and the serverName from the connectionstring in the webconfig?

And following links will provide you a solution to run a .sql script in C#
Run a .sql script file in C#
Run a .sql script files in C#

You can also use sqlcmd Utility.
Feature Pack for Microsoft SQL Server 2005
 
Share this answer
 
Comments
thatraja 8-Jan-12 3:04am    
Yep, Sqlcmd will do. 5!
Monjurul Habib 8-Jan-12 3:17am    
thank you
Uday P.Singh 8-Jan-12 4:21am    
5ed :)
Monjurul Habib 8-Jan-12 6:09am    
thank you
ProEnggSoft 30-Mar-12 6:03am    
+5
Load the script from the file into a string, and set it as the cmdText to an SqlCommand object.
Then call the appropriate command method: ExecuteScalar, ExecuteReader, or ExecuteNonQuery
 
Share this answer
 
you can use SMO (Sql server Management Object) by C#.
using the following library to execute this :

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


Sample Code like this :

FileInfo file = new FileInfo("ScriptFile.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = GetSqlConnection(); // create connection
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900