C# Prepared Statements

  • Thread starter Thread starter 5By5
  • Start date Start date
5

5By5

Hi all,

Can someone point me to an example of prepared statements being used in C#
ado.net? the MSDN site has examples using VB and my documentation in VS 2005
says "to be filled in" or something equally useful.

thanks in advance,
Pat
 
Do you mean the "Prepare" method on the (Sql)Command class? I'm documenting
that this week.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
William (Bill) Vaughn said:
Do you mean the "Prepare" method on the (Sql)Command class? I'm
documenting that this week.

that's exactly what i mean! :) this is the functionality that allows me to
write a statement like "select * from project where id=?" right?
 
The Prepare function is a throwback to the ODBC drivers that needed this to
tell the interface to "prepare" the SQL for execution. It's not needed in
ADO.NET and as far as I can tell (although my experiments aren't complete on
2.0) it does nothing (it's a noop) in the SqlClient provider.
If you want to build parameter queries you can, but it has nothing to do
with Prepare. Try building a Command object and set the Command text to
include named parameter markers
SELECT Author FROM Authors WHERE au_id = @idwanted
Next, setup a Parameters collection on the Command to manage the Parameter
cmd.Parameters.AddWithValue("@idwanted",15)

and execute.

I discuss this in my ADO.NET book (on the shelves now). While it does not
cover 2.0 it does cover these basics.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Bill,

thanks for the information. I do want parameterized queries. i was looking
at it from a Java perspective where we call these prepared statements.

Book? Book? well let me go check that out. It's the hitchhiker's guide one?

i'm very new to .net and so far i'm pretty pleased with everything i'm
finding. the wealth of resources is somewhat staggering.


thanks for the information and the quick responses,
Pat
 
No, the HHG for ADO.NET 2.0 is not done yet... (I keep stopping to answer
questions) ;)
I was referring to the ADO.NET Examples and Best Practices For C#
Programmers (Apress).
Sorry to hear about the Java thing... I think there are shots for that now.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Back
Top