Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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
Updated 25-Feb-13 3:03am
v3
Comments
Richard MacCutchan 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.

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();
 
Share this answer
 
try like this.... ..

C#
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;
    }
 
Share this answer
 
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...
 
Share this answer
 
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 ;
       }
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900