c# external sql server queries

We have a C# application, for some reasons i wont go into we are unable to use Entity Framework.

That being said I still prefer to have parameterized sql statements external from my application code, especially the large ones.  For most small CRUD based options I have just written a simple class to map the objects etc.  

What would be the best way to externalize the sql queries but still allow some dynamic parameters, Ill provide a simple example here

StringBuilder x = new StringBuilder();
x.AppendFormat("select * from Customers where CustomerId = {0}", this.CustomerId);
LVL 4
Brant SnowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Snarf0001Commented:
You can still use parameterized queries in an inline / ad hoc sql statement.  Added in the same way as you would a stored procedure.  Tough to give a real example without seeing the code (and how / what you're adding to the query), but something like this:

            SqlCommand com = new SqlCommand(string.Empty);

            StringBuilder x = new StringBuilder();
            x.Append("select * from Customers where CustomerId = @customerID");
            com.Parameters.AddWithValue("@customerID", this.CustomerId);

            //whatever other dynamic properties you have, added in the same way

            com.CommandText = x.ToString();
            com.ExecuteNonQuery();      //or whatever method you need

Open in new window


That's completely safe from injection attacks, and will pass the query to the sql engine AS parameters.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MlandaTCommented:
Agree with @Snarf. We make use of the Microsoft patterns and practices enterprise library - data access block (https://msdn.microsoft.com/en-us/library/dn169621.aspx). You can get this via nuget (https://www.nuget.org/packages/EnterpriseLibrary.Data/). It offers some improvements over directly writing data access code (e.g. it will manage opening and closing connections for you, connection pooling, and such). It is easy to use and works very well (tutorial: http://www.4guysfromrolla.com/articles/030905-1.aspx).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.