Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# MySqlDataReader missing one result

Posted on 2014-03-13
9
Medium Priority
?
546 Views
Last Modified: 2014-03-20
I am missing a result from my query.

using MySql.Data.MySqlClient;

public List<int> getNodeIDOfServerLike(String nodeName) {
         String fetchString = "SELECT NODE_ID FROM server_info_table WHERE NODE_NAME LIKE ?nodeName";
         Hashtable tempTable = new Hashtable();
         tempTable.Add("?nodeName", nodeName);
         openConnection();
         MySqlCommand cmd = createMySQLCommand(fetchString, tempTable);
         tempTable.Clear();
         MySqlDataReader dataReader = cmd.ExecuteReader();
         List<int> resultList = new List<int>();
         Console.WriteLine("Running: " + cmd.CommandText);
         while (dataReader.Read()) {
            Console.WriteLine("Got: " + dataReader["NODE_ID"].ToString());
            if (!String.IsNullOrEmpty(dataReader["NODE_ID"].ToString())) resultList.Add(int.Parse(dataReader["NODE_ID"].ToString()));
         }
         dataReader.Close();
         closeConnection();
         return resultList;
}

Open in new window


internal MySqlCommand createMySQLCommand(String commandText, Hashtable parameters) {
         if(SqlConnection != null) {
            MySqlCommand cmd = SqlConnection.CreateCommand();
            cmd.CommandText = commandText;
            cmd.Prepare();
            foreach (DictionaryEntry entry in parameters) {
               cmd.Parameters.Add((String)entry.Key, entry.Value);
            }
            return cmd;
         }
         return null;
      }

Open in new window


Result is:
Running: SELECT NODE_ID FROM server_info_table WHERE NODE_NAME LIKE ?nodeName
Got: 189

When I run the the same query directly agains the mysql server, I get two results:
189
457

That would be correct. But when I run the code above, I only get the first result. So what am I doing wrong here?
0
Comment
Question by:itnifl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 750 total points
ID: 39925996
case sensitive vs insensitive ?
0
 
LVL 2

Author Comment

by:itnifl
ID: 39926029
It doesn't look that way. It doesn't matter what case I supply the nodeName in. Also I get the other ID(457)  if I supply the whole node name for that ID.
0
 
LVL 2

Accepted Solution

by:
itnifl earned 0 total points
ID: 39926089
Actually, this is the fix:
tempTable.Add("?nodeName", nodeName + "%");

Open in new window

0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 750 total points
ID: 39926130
>>Actually, this is the fix:

Odd - I don't see why that would make any difference between the code and the direct approach (unless you didn't use the identical SQL statement that is when testing it directly)
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 39926838
Why are you using a question mark in your parameter name? I've always seen an @ symbol used.

i.e.

http://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-parameters.html
0
 
LVL 2

Author Comment

by:itnifl
ID: 39928865
When I test the query directly I write as follows:
SELECT NODE_ID FROM server_info_table WHERE NODE_NAME LIKE 'OHTHATSERVERNAME%';

When I in the code use:
SELECT NODE_ID FROM server_info_table WHERE NODE_NAME LIKE ?nodeName%
it won't accept ?nodeName as a parameter.

I don't remember or know why I use a question mark as a parameter mark, this code was written some time ago. I am guessing the choice of a parameter mark is optional?

Obviously the wild card has to be treated as a part of the parameter value. I can't say that this is very obvious or intuitive.
0
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 750 total points
ID: 39929004
>>When I test the query directly I write as follows:
SELECT NODE_ID FROM server_info_table WHERE NODE_NAME LIKE 'OHTHATSERVERNAME%';
When I in the code use:
>>SELECT NODE_ID FROM server_info_table WHERE NODE_NAME LIKE ?nodeName%


That isn't what you posted in the question - there was no trailing % in the code based SQL query.

So, basically you got different results because you used different queries.
0
 
LVL 2

Author Comment

by:itnifl
ID: 39931191
This does not work in the code:
SELECT NODE_ID FROM server_info_table WHERE NODE_NAME LIKE ?nodeName%

And this does not work in the code:
SELECT NODE_ID FROM server_info_table WHERE NODE_NAME LIKE ?nodeName

However, this works:
SELECT NODE_ID FROM server_info_table WHERE NODE_NAME LIKE ?nodeName
and then adding the % with the parameter value:
tempTable.Add("?nodeName", nodeName + "%");

In the question I am not seeking why there are differences, I am asking how to get the result I want(all results from the query). The answer is to supply % with the parameter value, for some reason.
0
 
LVL 2

Author Closing Comment

by:itnifl
ID: 39941781
Tanks for participating! :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question