开发者

Put the Query result of MySql in to a C# list or array?

开发者 https://www.devze.com 2023-01-30 07:02 出处:网络
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'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开发者_如何学Python 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;
    }       


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();
    }
 }  
0

精彩评论

暂无评论...
验证码 换一张
取 消