473,671 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OleDb query with positioned parameters

5 New Member
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 2198
mldisibio
190 Recognized Expert New Member
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 Recognized Expert Specialist
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
melryin
5 New Member
@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 Recognized Expert Specialist
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
3420
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 but they fail at the database. To fix this they Parameters.Add must be called in the ORDER they are in the SQL STATEMENT. This is confusing and bad.
9
4753
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 DataSet; and all I'm doing is showing one record in text fields, allowing the user to modify the text fields, and then updating the database again when the user clicks the Save button. The fields already show the correct data record since I have...
1
2313
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 that the record was inserted at. I had this, but it gives me a "data type mismatch" error on run. Dim cmdPostQuestion As New OleDb.OleDbCommand("INSERT INTO hd_questionsasked (askedBy,questionText,dateAsked) VALUES (?,?,?)") ...
3
8823
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 don't think I made any changes to either page other than changing the user control that creates the header). Server Error in '/myApp' Application. ---------------------------------------------------------------------------- ----
2
2849
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 the db to SQL SERVER and this went well. Next thing I changed the connection string from OLEDB for MS ACCESS to OLEDB for SQL SERVER. I expected this to work resonably well, however I found that it never worked
0
5651
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 System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet) at TryThis.Form1.save() in C:\Documents and Settings\Nick\My Documents\...
0
8483
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8824
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7444
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6236
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2818
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.