benshepherd icon

MySql Example

benshepherd | PRO | 02/09/13 11:50:46 AM UTC | 0 ⭐ | 281 👁️ | Never ⏰ | []
C# |

710 B

|

None

|

0 👍

/

0 👎

static void Main(string[] args)
{
    string[] users = GetMySqlResults("SELECT users FROM myTable");
}
 
static string[] GetMySqlResults(string query) {
    string[] results = new string[]{};
    
    try
    {
        using(MySqlConnection con = new MySqlConnection("server=localhost;user=username;password=12345;database=mydb"))
        {
            con.Open();
            using(MySqlCommand cmd = new MySqlCommand(query, con))
            {
                using(MySqlDataReader reader = cmd.ExecuteReader())
                {
                    results = new string[reader.FieldCount];
                    for(int i = 0; i < results.Length; i++)
                    {
                        results[i] = reader[i].ToString();
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    
    return results;
}

Comments