Solved

C# MySqlDataReader missing one result

Posted on 2014-03-13
9
520 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 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 375 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 375 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 125 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 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 375 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

733 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