Add item to resource? Or library?

  • Thread starter Thread starter Ivan
  • Start date Start date
I

Ivan

Hello group.

Here is what I like to do.

#1. Add ".SQL" file to project
#2. Being able to load contents of this file into string variable.

So, I want .SQL to be compiled into executable and being able to retreive
contents for use inside program.

To explain what I'm doing:
I have app that interacts with DB. Some objects that needed is Stored
procedures and temp tables. My idea is instead of supplying separate DB
install - I rather have client refresh those objects on every startup. It's
utility and this approach will work just fine.

I can probably add this SQL like a string constants but it's PITA to manage.
Would be much prettier if I had it in SQL files so I can edit them in SQL
Management Studio

Thank you,
Ivan
 
Ivan,

If you use Visual Studio 2005 (that is, .NET 2.0) or later you can make use
of the Resources section of your Project Properties. Go there and choose the
expando button next to "Add Resource". This will allow you to add an
existing file (the .SQL one). Visual Studio will recognize that it as a text
file and you can get the string contents like this:

string sqlContents = Properties.Resources.{Resource/FileName};
 
If you use Visual Studio 2005 (that is, .NET 2.0) or later you can make
use of the Resources section of your Project Properties. Go there and
choose the expando button next to "Add Resource". This will allow you to
add an existing file (the .SQL one). Visual Studio will recognize that it
as a text file and you can get the string contents like this:

string sqlContents = Properties.Resources.{Resource/FileName};

Most of the things I add via Add Existing File get classified as byte
arrays, not strings (even when they're text files, like XML). Or does the
IDE convert it for you?
 
I suppose the IDE does a quick check of the content to determine the best
matching data type. Not 'catching' the text or XML files might be related to
the encoding those files use.
 
I found another way.

I add file, mark it to "Embed"

Then write code:

StreamReader streamReader = new
StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("Mynamespace.Folder.FileName.Extention"));
string sSQL = streamReader.ReadToEnd();

SqlCommand sc = new SqlCommand(sSQL, DynamicsGPConnection);
sc.ExecuteNonQuery();
 
That's exactly what I was avoiding with the Resources method. But I presume
you use .NET 1.1 (that's how I would do it there).

The reasons I avoid this is because you have to hardcode the fully-qualified
resource name, use kind of dirty Reflection code and StreamReaders for
something VS can present to you in a straight-forward object-oriented way.
 
I see your point, I changed it to do like you suggested, intellisence, etc.
Nice.

Now I realized that if I want to have unlimited number of scripts I can just
name them "1.sql", "2.sql" and so on and add to resources. Then method I
specified can be used to pull files using reflection and then it would just
error out when all files done.
 
Back
Top