locked
C# 3.0 - Store and retrieve SQL strings in external file RRS feed

  • Question

  • Hi guys,

    I need to store several SQL strings, and later retrieve them.

    Something like:

     

    sqlString1 = "SELECT * FROM Table1"

    sqlString2 = "SELECT * FROM Table2"

    sqlString3 = "SELECT * FROM Table3"

    sqlString4 = "SELECT * FROM Table4"

    sqlString50 = "SELECT * FROM Table50"

     

    Is there any kind of file (maybe XML?) to store those strings and later call them from the code (I'll need to interact with the user and then add/change some parameters) ?

    Is there any good tutorial?

    Thanks in advance.

    Aldo.

     

    Thursday, May 1, 2008 10:51 AM

Answers

  • But SQL strings are just that... strings! So one option (for user-editable) is to add a .settings file, with a user-scoped string setting, and put the value in (noting you can use the pull down for a larger editor). You can do the same with a resource (resx) file, except the designer doesn't give you a pull-down editor - but you can also just drop a text file into the project, and drag *that* into the resx designer.

     

    (I'm still very nervous about the general idea, though...)

    Thursday, May 1, 2008 11:52 AM

All replies

  • Well, resx is good for fixed strings (perhaps with {0} {1} etc tokens), and config / settings files are good for user-maintainable values - but I'm not entirely sure I'd use either to store raw SQL; and I'd certainly not trust user-generated SQL strings ("Bobby Tables", anyone?).

     

    You have to be very careful handling SQL in this way; and a reminder: don't embed values into the string - use named parameters - i.e. "WHERE Foo = @param1" (and add a parameter called "param1").

    Thursday, May 1, 2008 11:08 AM
  • Yeap, I intent to use parameterized queries, but I am looking for a good way to store the "basic" TSQL string I need.

    I didn't see any possibility to store SQL strings in the config.

    I did it using an access db, but the strings are too long and getting difficult to operate with access in those conditions.

     

    Thursday, May 1, 2008 11:22 AM
  • But SQL strings are just that... strings! So one option (for user-editable) is to add a .settings file, with a user-scoped string setting, and put the value in (noting you can use the pull down for a larger editor). You can do the same with a resource (resx) file, except the designer doesn't give you a pull-down editor - but you can also just drop a text file into the project, and drag *that* into the resx designer.

     

    (I'm still very nervous about the general idea, though...)

    Thursday, May 1, 2008 11:52 AM
  • This far it looks that a resources file is the best option.

    Thanks Marc.

     

    Thursday, May 1, 2008 1:35 PM