advertisement

Forum => Article discussions => Using parameterized SQL queries

Jump to:

Next Oldest | Next Newest

6/7/2004 11:22:21 PM Link | Reply | Edit | Quote

uber:ASP.Net

Location: | Joined: 1/1/2000 | Posts: 12 | Online

Article discussion for:
Using parameterized SQL queries

12/9/2004 12:46:46 AM Link | Reply | Edit | Quote

sduvy

Profile Send Private Message Send E-Mail
Location: | Joined: 9/22/2004 | Posts: 10 | Offline

Hi Jeff,

I am just beginning to work with parameterized queries and unforunately I'm starting using Access as my database. I am having particular problems with date comparison queries such as the following:

SELECT * FROM Calendar WHERE BeginDate > @FirstDay

The result is backwards! In other words, I am getting all dates that are LESS THEN the @FirstDay value. This happens within Access itself as well. The only way I can get it to work within Access is to do the following:

SELECT * FROM Calendar WHERE BeginDate > DateValue('12/1/2004')

Unfortunately, I can't figure out how to parameterize a query in vb.net that will get the correct result.

I have your access.cs file from PopForums and I've been using it as a guide, and it's been very helpful. What are some of the quirks of working with Access that I should expect?

One that I've already come across has to do with Memo fields. I couldn't get a parameterized INSERT statement to run if the Memo field wasn't first in the list.

Thanks in advance,
Stuart

12/9/2004 12:35:41 PM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Send E-Mail Web Site AOL Instant Message Yahoo Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline

I think with Access you simply use question marks, and then add the parameters in order. So your SQL would be:

SELECT * FROM Calendar WHERE BeginDate > ?

A regular Access user could probably back me up on this. I'm not positive.


Jeff 'Jones' Putz
POP World Media, LLC
Maximizing ASP.NET

12/21/2004 10:39:27 PM Link | Reply | Edit | Quote

sduvy

Profile Send Private Message Send E-Mail
Location: | Joined: 9/22/2004 | Posts: 10 | Offline

Thanks. Actually I was using the question marks so that wasn't the problem. I gave up trying to get it to work myself. Instead, I downloaded Pete Wilson's ORMapper assembly which is completely database agnostic. I pass the same code to the methods regardless of the database on the back end. A great tool for $50.

1/5/2005 2:12:21 PM Link | Reply | Edit | Quote

ace999

Profile Send Private Message Send E-Mail
Location: | Joined: 1/5/2005 | Posts: 2 | Offline

I'm trying to do a parameterised query using the following code

in c# below... the db is oracle8i....

the code below is not working at all... any ideas

string objConn = "Provider = MSDAORA;User ID=103109798;Password=password;Data Source=orabis;";
OleDbConnection myConnection = new OleDbConnection(objConn);
string commandString = "INSERT INTO users(username,password)VALUES(:username,:password)";
OleDbCommand myCommand = new OleDbCommand(commandString, myConnection);
myCommand.Parameters.Add(":username", txtUsername.Text)
myCommand.Parameters.Add(":password", txtPassword.Text)
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();

1/5/2005 3:39:07 PM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Send E-Mail Web Site AOL Instant Message Yahoo Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline

By "not working" you mean what? Do you get an error? I've not used Oracle.


Jeff 'Jones' Putz
POP World Media, LLC
Maximizing ASP.NET

1/5/2005 4:11:12 PM Link | Reply | Edit | Quote

ace999

Profile Send Private Message Send E-Mail
Location: | Joined: 1/5/2005 | Posts: 2 | Offline

the page just hangs, it does not do anything,

basically i want the user to be able to enter their username

with a ' or ; without affecting the sql statement.

1/6/2005 2:03:31 PM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Send E-Mail Web Site AOL Instant Message Yahoo Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline

First off, I'd use the Oracle classes instead of OleDb. Second, I don't know how you specify parameters in Oracle, but if the page doesn't appear to be doing anything then you should try to use whatever tools Oracle has to see what chatter is going on between the site and the database.


Jeff 'Jones' Putz
POP World Media, LLC
Maximizing ASP.NET

2/10/2005 1:03:49 AM Link | Reply | Edit | Quote

Div

Profile Send Private Message Send E-Mail
Location: | Joined: 2/10/2005 | Posts: 1 | Offline

Ace, did you get it to work? I've come across this before and like Jeff said, use the Oracle provider (you'll need to add a reference to it.)

Then import it: system.data.oracleclient

I've had to develop w/ asp.net and Oracle 8i before, and if you have questions, feel free to email me.

Ciao!

8/23/2005 12:12:11 PM Link | Reply | Edit | Quote

du8die

Profile Send Private Message Send E-Mail
Location: Southeast Wisconsin | Joined: 1/16/2004 | Posts: 126 | Offline

If you're like me, and use MySQL, the code is like this...

sql = "insert into table (field) values (?)"

cmd.parameters.add("", _TheField)

Parameters must be added in the order in which they appear in the query string.

d8


The cause of the problem is:
The POP server is out of Coke .

5/10/2006 8:53:37 AM Link | Reply | Edit | Quote

du8die

Profile Send Private Message Send E-Mail
Location: Southeast Wisconsin | Joined: 1/16/2004 | Posts: 126 | Offline

Does anyone know if it's possible to do a "Like" query using parameters?

Select * from table where field1 like @Param1

?

Thanks,

D8


The cause of the problem is:
The POP server is out of Coke .

5/10/2006 11:52:34 AM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Send E-Mail Web Site AOL Instant Message Yahoo Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline

SELECT * FROM table WHERE field LIKE '%' + @param + '%'


Jeff 'Jones' Putz
POP World Media, LLC
Maximizing ASP.NET

Forum => Article discussions => Using parameterized SQL queries

Please login or register to post.