Question about SELECT

Question about SELECT

Postby gamito » Fri 02 May 2008 13:17


I have this C# code to make a SELECT that follows my signature.

I'm sorry for the lame question, but although I've been a Linux and DB administrator for 15 years, I'm a total newbie to C# and MyDirect.NET.

I've read MyDirect manual and FAQ, but I didn't found what I'm looking for.

My question is, this SELECT statement returns a single value, a string.
How do I fetch it, i. e., how do I put the value coming from the SELECT statement into a variable ?

Any help would be appreciated.

Warm Regards,
Mário Gamito

public void selectMyValue ()


string MyConnectionString;

if (myConnectionString="")
myConnectionString="User Id=vpopmail; Host=; Database="vpopmail"; Password="secret";

string myResult = "SELECT sha FROM vpopmail WHERE pw_name = 'gamito'";

MySQLCommand MyCommand = new MySQLCommand(myResult);

myCommand.Connection = myConn;


catch (Excepction MyException)
Response.Write("Connection Error: " + MyException.Message);


Posts: 2
Joined: Mon 18 Feb 2008 17:13

Postby Alexey.mdr » Mon 05 May 2008 14:24


Well it's quite easy.
You might use myCommand.ExecuteNonQuery(); with not SELECT statements.
If you would like to get data from a database you might want to use a MySqlDataReader instance.
Please see the following sample:
Code: Select all
string mySelectQuery = "SELECT sha FROM vpopmail WHERE pw_name = 'gamito'";
            MySqlConnection myConnection = new MySqlConnection(myConnString);
            MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection);
            MySqlDataReader myReader = myCommand.ExecuteReader();
            string result;
            try {
                while (myReader.Read()) {
                    result = myReader.GetString(0);
            } finally {               
Have you seen provided samples in the Start menu? They are really helpful.
Posts: 729
Joined: Thu 13 Dec 2007 10:24

Return to dotConnect for MySQL