473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

OleDb query with positioned parameters

Hi,

I'm a newbie in C#, I wrote an OLEDb query with positioned parameter. It seems not working. Can anyone please point out where I've done wrong? Thanks in advance.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. using (OleDbConnection conn = new OleDbConnection(connString))
  2. {
  3.              String key = textbox1.text;
  4.              string cmd = "Select * from [" + filename + "] Where " + fieldname + " like ?";
  5.              OleDbCommand command= new OleDbCommand(cmd, conn);
  6.              OleDbParameter parameter = new OleDbParameter("@key",OleDbType.VarChar,30);
  7.              parameter.Value = "'%"+key+"%'";
  8.              command.Parameters.Add(parameter);
  9.              conn.Open(); 
  10.              OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmd, conn);
  11.              OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);
  12.              DataTable table = new DataTable();         
  13.              table.Locale = System.Globalization.CultureInfo.InvariantCulture;
  14.              dataAdapter.Fill(table);
  15. }
Jan 5 '09 #1
4 2190
mldisibio
190 Expert 100+
Try your code without creating the CommandBuilder. Comment out:
Expand|Select|Wrap|Line Numbers
  1. OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);
I am not positive, but I think that statement will overwrite your original command text.
Jan 5 '09 #2
Curtis Rutland
3,256 Expert 2GB
Well, the CommandBuilder line is actually doing nothing...well, nothing useful. All you are doing is instantiating a commandbuilder object, not actually using it in any way. So you should remove it.

I think that the parameter may be your problem. Try building your command like this:
Replace line 4 with this:
Expand|Select|Wrap|Line Numbers
  1. string cmd = String.Format("Select * from [{0}] where {1} like '%{2}%'", filename, fieldname, key);
This should take care of the filename, fieldname, and key, all at once. That will let you eliminate lines 6, 7, and 8.
Jan 5 '09 #3
@insertAlias
You are absolutely correct, the parameter is my problem. I tried your way.It works =D

But I use parameter method is to escape escape-characters in key, which is input by user. If I do it in this way, it won't escape.
Jan 6 '09 #4
Curtis Rutland
3,256 Expert 2GB
Try using named parameters instead of the ? symbols.

Expand|Select|Wrap|Line Numbers
  1. string sql = "select field from table where param1 = @param1"
Use the correct parameter name in the query.
Jan 6 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: | last post by:
Hi, I was performing SQL UPDATE queries and I notice that they SUCCEED on the ExecuteNonQuery() call with NO exceptions raised BUT they fail at the Database. They say they succeed in the code...
9
by: Pam Ammond | last post by:
I need the code to update the database when Save is clicked and a text field has changed. This should be very easy since I used Microsoft's wizards for the OleDBAdapter and OleDBConnection, and...
1
by: Brian Henry | last post by:
I have an access database, and one of the fields in the table I am inserting into has a date/time data type. What is the correct OleDb data type to insert the date and time that it is at the moment...
3
by: Brian Foree | last post by:
I am developing an ASP.NET application that uses Access 2000 as its backend, and have just started getting the following error on 2 ASP.NET pages that had been working until late last week (and I...
2
by: Martin | last post by:
Hi, I currently have an application that connects to an MS ACCESS database. This application uses an OLEDB connection string for MS ACCESS. Now, I'd like to upsize the application so I converted...
0
by: NicK chlam via DotNetMonster.com | last post by:
this is the error i get System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.Common.DbDataAdapter.Update(DataRow dataRows, DataTableMapping tableMapping) at...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.