1

I've done some searching for the answer, but only find a lot of PHP answers. I need this for a C# application. Can any one point me in the right direction?

I'm doing this, but this only returns the first row into my variable:

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

1 Answer 1

5

ExecuteScalar is designed to only return one (thus scalar) value:

Executes the query, and returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored.

Try using ExecuteReader. Example:

public void CreateMySqlDataReader(string mySelectQuery, MySqlConnection myConnection) 
 {
    MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection);
    myConnection.Open();
    MySqlDataReader myReader;
    myReader = myCommand.ExecuteReader();
    try
    {
      while(myReader.Read()) 
      {
        Console.WriteLine(myReader.GetString(0));
      }
    }
    finally
    {
      myReader.Close();
      myConnection.Close();
    }
 }  
Sign up to request clarification or add additional context in comments.

1 Comment

An update with new language features could be nice, this is 9 years old now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.