Alternative way to write queries: Parameterized queries .... i guess

  • Thread starter Thread starter weird0
  • Start date Start date
W

weird0

i write query in this manner by contatenting a string:

cmd.CommandText = "SELECT acc_name FROM Account_Information
where user_id='" + User_Id + "'";


Is there any other way to do it....

please tell me how to do so and some good link related to that?
 
Doing that is a big no-no for both protection from SQL injection and
for performance. See http://davidhayden.com/blog/dave/archive/2005/10/24/2528.aspx
for info on that.

For your basic CRUD type operations parameterized queries are the way
to go
There are a bunch of example on the net such as
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx


Other alternatives include object relational mapper such as LLBLGen
Pro, NHibernate. Theses are nice for big projects. At the top level
they make your query look like an object and then you perform actions
against that object.

The last alternative would be using stored procedures for your CRUD
based queries. http://www.codinghorror.com/blog/archives/000117.html
gives a good description on the pro/cons of using them for CRUD
 
weird0 said:
i write query in this manner by contatenting a string:

cmd.CommandText = "SELECT acc_name FROM Account_Information
where user_id='" + User_Id + "'";


Is there any other way to do it....

Yes - the above has huge dangers with respect to SQL injection attacks,
as well as making it harder for the query optimiser.
please tell me how to do so and some good link related to that?

It depends on which database provider you're using, but if you look at
SqlCommand.Parameters you'll see an example for the SQL Server
provider.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top