Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MySQL C#4.0
Hello,
 
I wanted to store the result of a mysql query into a string variable in C#. I tried using the following code but it is not working. Can anyone please suggest a solution.
MySqlCommand comm = new MySqlCommand("SELECT Employee_Name FROM server_side_user WHERE Employee_Username="+ "'" + currentUser + "'" + "", conn);           
            
MySqlDataReader currentLoggedInUser = comm.ExecuteReader();
 
while (currentLoggedInUser.Read())
{
    string user = currentLoggedInUser.GetString(0);
}
 
When I run the above code, the variable currentUsername will contain the value false. Can anybody suggest a proper code?
 
I want to store the result of the query into the variable user.
Posted 25-Feb-13 2:53am
Edited 25-Feb-13 3:03am
v3
Comments
Richard MacCutchan at 25-Feb-13 9:19am
   
It looks like your SELECT statement did not return any rows, so the first value returned is zero (or false). Check the actual content of your command and the values returned.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi,
As said by OriginalGriff above, you should always use parametrized query to avoid SQL Injection.
 
And if you know your query will return single value, you should use ExecuteScalar method instead of ExecuteReader.
 
Ex-
conn.Open();
string EmployeeName=comm.ExecuteScalar().ToString();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try like this.... ..
 
Query("SELECT SaksNummer FROM casetracking")
 
    public static string Query(string query)
    {
        string x;
        mysqlCon.Open();
        cmd = new MySqlCommand(query, mysqlCon);
        x = cmd.ExecuteScalar().ToString();
        mysqlCon.Close();
        return x;
    }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

First off, don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Second, if your query worked, then you will load the last of any returned values into your "user" variable (because it is in a loop and the last value will overwrite all the others) - and then you will throw it away without using it because "user" goes out of scope at the end of the loop.
 
At no point do you modify or even reference a variable called currentUsername so it will have the same value at the end as it did at the start...
 
I think you need to sit down and think about what you are tryingto achieve here - because this looks a lot like either very muddled thinking, or the old "chuck it together and hope it works by magic" approach. Neither of these are a good idea...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try This ->
 static string Put_In_String()
        {
            SqlConnection myConnection = new SqlConnection();
 
                myConnection.Open();
 

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select Query";
			var result = cmd.ExecuteReader();
			var myArray = new string[,] {};
			var counter = 0;
 
			while (result.Read())
			{
				myArray[counter, 0] = (string) result["First Column Name"];
				myArray[counter, 1] = (string) result["Second Column Name"];
				// etc...

				counter++;
			}
            result.Close();
            myConnection.Close();
            return myArray ;
        }
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 398
1 CPallini 333
2 George Jonsson 270
3 OriginalGriff 159
4 Prasad Khandekar 135
0 OriginalGriff 4,613
1 CPallini 3,340
2 Sergey Alexandrovich Kryukov 2,771
3 George Jonsson 2,254
4 Gihan Liyanage 2,020


Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100