Click here to Skip to main content
Sign Up to vote bad
good
See more: MySQLC#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:53
Edited 25 Feb '13 - 3:03

Comments
Richard MacCutchan - 25 Feb '13 - 9:19
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.

4 solutions

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  
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  
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  
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  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 449
1 Arun Vasu 253
2 OriginalGriff 210
3 CPallini 163
4 Aarti Meswania 158
0 Sergey Alexandrovich Kryukov 10,129
1 OriginalGriff 7,749
2 CPallini 4,181
3 Rohan Leuva 3,482
4 Maciej Los 2,999


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid