I would like to know if it is possible to include a sql script in clickonce, I need to update an application that connects locally to sql server, and within the same update it is necessary to update some stored procedures, it is possible to execute these scripts with click once, or I will have Connect to the database and execute them manually, the application clickonce is stored in a server in the cloud to be downloaded
1 Answers
Store the stored procs or whatever scripts you need, as "Embedded Resource" files in the project. Then you can just load them when the program runs and execute them against the SQL Server using standard ADO.NET commands.
Reading embedded resource text file: How To Read Embedded Resource File
You just need to retrieve your script(s) and execute using code similar to the following:
// Read stored procedure script from embedded resource or simply assign in code, for example.
string scriptText = "CREATE PROCEDURE Test AS BEGIN SELECT 'Test'; END";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = scriptText;
cmd.ExecuteNonQuery();
}
}
For your script, you might want to write it so it can handle situations where the stored procedure already exists (for example: if it exists, drop it first, then CREATE PROCEDURE). If you have any permissions that need to be handled (eg. GRANT), they can be added to the script as well.
Typically, you would want to separate each stored procedure into a separate embedded resource text file and just loop through all the files and execute each one using SQL code like above. You might want to put these "scripts" in a separate assembly (aka Project) so it is easy to read them using the previous link.

- 935
- 9
- 15
-
Do you have any documentation link on how to execute these files within the app? – Jorge Torselli Jun 29 '17 at 01:15
-
@JorgeTorselli - I add some details above. Hopefully that helps you get started. – Doug Knudsen Jun 30 '17 at 03:39
-
Link to another issue on reading embedded resources, if you need help with that: https://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file – Doug Knudsen Jul 04 '17 at 15:34
-
do I have to create a plain text or add a class to click once for the deployment to read, does it read at first or I have to set the time for reading the embdded resource during deployment – Jorge Torselli Jul 11 '17 at 03:46
-
The script files are just plain text marked as Embedded Resources in the deployed project/DLL. This simply gives you the ability to access the files where-ever the code is running without having to worry about extra files. Plus, the "scripts" are built into the DLL so you know the right ones are being used. As far as when the scripts are ran, that is up to you. It could be automatic when the program is ran (check the database for a version or something and run the appropriate scripts) or provide a manual function in the program to "check" the database or run scripts etc. – Doug Knudsen Jul 11 '17 at 17:36