tags:

views:

277

answers:

1

Newbie question : I would like to store lengthy .sql scripts in my solution and execute them programmatically. I've already figured out how to execute a string containing my sql script but I haven't figured out how to read the string from a file that would be stored in the solution (under a /Scripts subfolder for example).

Many thanks,

+2  A: 

First, edit the .sql file's properties so that it will be embedded as a resource.

Then use code similar to the following to retrieve the script:

string commandText;
Assembly thisAssembly = Assembly.GetExecutingAssembly();
using (Stream s = thisAssembly.GetManifestResourceStream(
      "{project default namespace}.{path in project}.{filename}.sql"))
{
   using (StreamReader sr = new StreamReader(s))
   {
      commandText = sr.ReadToEnd();
   }
}
Abraham Pinzur
Thank you sir !!
Yann Semet
I have no idea why the hell people still do this. FFS, resources are accessible via a strongly typed class. `[default namespace].Properties.Resources.WhateverYouCalledIt. This whole manifest resource stream bullshit needs to get stamped out.
Will