I'm writing a database client program in C#, and it accesses a MS SQL V7
database. In one part of my program I am using a SqlCommand class to run a
SQL Statement, the command text uses parameters, but these do not seem to be
functioning (as it does not find the record), it's probably some really
stupid error I've made, but I've spend several hours trying to find it and
its now driving me crazy. Also something very similar else where in the
program works fine.
The table I'm using is called Suppliers and it has a field called
AccountCode and on of the records has the data value of Rice in the
AccountCode field.
If I write the statement so that it is as follows, the code finds the
record:
sqlCommand.CommandText = "SELECT * FROM " + "[" + this.tableName + "]" +
" WHERE AccountCode = 'Rice';";
But when using parameters it doesn't work, here is the code with parameters:
SqlCommand sqlCommand = new SqlCommand("", dbConnection);
SqlDataReader dReader;
sqlCommand.CommandType = CommandType.Text;
SqlParameter param1 = new SqlParameter("@searchField", "AccountCode");
sqlCommand.Parameters.Add(param1);
SqlParameter param2 = new SqlParameter("@searchString", "Rice");
sqlCommand.Parameters.Add(param2);
sqlCommand.CommandText = "SELECT * FROM " + "[" + this.tableName + "]" +
" WHERE @searchField = '@searchString';";
// Also tried the @searchString with out the ' ' each side of it
//" WHERE AccountCode = 'Rice' ; "; // It works if modifed to
this
dReader = sqlCommand.ExecuteReader();
if(dReader.HasRows == true)
{
.....
}
Anyone have any ideas why the parameter version doesn't work?
No exceptions are thrown, the data reader just doesn't return any data.
Thank You for any help,
Grant 2 10844
Parameters are used with stored procedures.
I do not understand why are you trying to use paramaters with
CommandType.Text.
"Grant Stanley" <ag**@kent.ac.uk.delete> wrote in message
news:rQ********************@brightview.com... I'm writing a database client program in C#, and it accesses a MS SQL V7 database. In one part of my program I am using a SqlCommand class to run a SQL Statement, the command text uses parameters, but these do not seem to
be functioning (as it does not find the record), it's probably some really stupid error I've made, but I've spend several hours trying to find it
and its now driving me crazy. Also something very similar else where in the program works fine.
The table I'm using is called Suppliers and it has a field called AccountCode and on of the records has the data value of Rice in the AccountCode field.
If I write the statement so that it is as follows, the code finds the record: sqlCommand.CommandText = "SELECT * FROM " + "[" + this.tableName + "]" + " WHERE AccountCode = 'Rice';";
But when using parameters it doesn't work, here is the code with
parameters: SqlCommand sqlCommand = new SqlCommand("", dbConnection); SqlDataReader dReader; sqlCommand.CommandType = CommandType.Text;
SqlParameter param1 = new SqlParameter("@searchField", "AccountCode"); sqlCommand.Parameters.Add(param1); SqlParameter param2 = new SqlParameter("@searchString", "Rice"); sqlCommand.Parameters.Add(param2);
sqlCommand.CommandText = "SELECT * FROM " + "[" + this.tableName + "]" + " WHERE @searchField = '@searchString';"; // Also tried the @searchString with out the ' ' each side of it //" WHERE AccountCode = 'Rice' ; "; // It works if modifed to this dReader = sqlCommand.ExecuteReader(); if(dReader.HasRows == true) { .... }
Anyone have any ideas why the parameter version doesn't work? No exceptions are thrown, the data reader just doesn't return any data.
Thank You for any help, Grant
My guess is that you can use parameters only for literal values and not for
field names. You can't treat your query as a sort of macro expression where
parameter values substitute their placeholders no matter what they mean.
Rather parameters are used in run-time to pass values to the Select
statements or stored procedures.
HTH,
Eliyahu
"Grant Stanley" <ag**@kent.ac.uk.delete> wrote in message
news:rQ********************@brightview.com... I'm writing a database client program in C#, and it accesses a MS SQL V7 database. In one part of my program I am using a SqlCommand class to run a SQL Statement, the command text uses parameters, but these do not seem to
be functioning (as it does not find the record), it's probably some really stupid error I've made, but I've spend several hours trying to find it
and its now driving me crazy. Also something very similar else where in the program works fine.
The table I'm using is called Suppliers and it has a field called AccountCode and on of the records has the data value of Rice in the AccountCode field.
If I write the statement so that it is as follows, the code finds the record: sqlCommand.CommandText = "SELECT * FROM " + "[" + this.tableName + "]" + " WHERE AccountCode = 'Rice';";
But when using parameters it doesn't work, here is the code with
parameters: SqlCommand sqlCommand = new SqlCommand("", dbConnection); SqlDataReader dReader; sqlCommand.CommandType = CommandType.Text;
SqlParameter param1 = new SqlParameter("@searchField", "AccountCode"); sqlCommand.Parameters.Add(param1); SqlParameter param2 = new SqlParameter("@searchString", "Rice"); sqlCommand.Parameters.Add(param2);
sqlCommand.CommandText = "SELECT * FROM " + "[" + this.tableName + "]" + " WHERE @searchField = '@searchString';"; // Also tried the @searchString with out the ' ' each side of it //" WHERE AccountCode = 'Rice' ; "; // It works if modifed to this dReader = sqlCommand.ExecuteReader(); if(dReader.HasRows == true) { .... }
Anyone have any ideas why the parameter version doesn't work? No exceptions are thrown, the data reader just doesn't return any data.
Thank You for any help, Grant
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Joe Lui |
last post by:
Hello everyone,
I am new to PHP and web server in general so please bear with me if
it's an obvious mistake.
My problem is that parameters entered in URL do not update php
variables. I am...
|
by: Arpan |
last post by:
Microsoft advises not to pass parameters to the Command object in the
Execute statement. Why?
Thanks,
Arpan
|
by: Gerry Abbott |
last post by:
Hi all
I'm converting a db from Acc97 to 2000. Everything works, except assigning
of a qdf object containing parameters, to a recordset.
The db, recordset, and qrydef objects are all referenced...
|
by: Zlatko Matić |
last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL
Server) and that we want to execute some parameterized query as a
pass.through query. How can we pass parameters to the...
|
by: Jack |
last post by:
I have the following:
* An OLEDBCommand with command text "SELECT CAMPAIGN,
DAY_OUT WHERE (CAMPAIGN LIKE '@campaign')"
* A DataAdapter that point the select to the above command
* A data grid...
|
by: pjcraig |
last post by:
This is driving me crazy! I have a form that a user will access from
another form by selecting the item that they wish to view. When they
open the new form, I pass through the id of the item they...
|
by: nigeljordan |
last post by:
Hi
I’m having problems with the following method…
It works for the ‘@user’ bit (i.e. it takes in the user variable and works within the sql command).
But the @column variable still is not...
|
by: nigeljordan |
last post by:
Hi
I’m having problems with the following method…
It works for the ‘@user’ bit (i.e. it takes in the user variable and works within the sql command).
But the @column variable still is not...
|
by: Hexman |
last post by:
Hello All,
Well I'm stumped once more. Need some help. Writing a simple select and update program using VB.Net 2005 and an Access DB. I'm using parameters in
my update statement and when trying...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
| |