Решил написать свой класс для работы с MySQL нахожу эту штуку очень полезной т.к. если нужно быстро начать работу с базой данных, то думаю это то что нужно. Можете скачать весь проект и попробовать его в деле. Позже планирую выложить на какой нибудь VCS типа github или bitbucket так что следите за обновлениями поста. Ниже привожу код.
using System; using System.Collections.Generic; using MySql.Data.MySqlClient; using System.Data; namespace MySQLClass { public class MySqlWrapper { private MySqlCommand command; private MySqlConnection connection; private string mySqlConnectionString; private int executionTimeOut; public List<string> ErrorLog; public MySqlWrapper(string init) { this.ErrorLog = new List<string>(); this.executionTimeOut = int.MaxValue; this.mySqlConnectionString = init; try { this.connection = new MySqlConnection(this.mySqlConnectionString); } catch (Exception ex) { this.ErrorLog.Add("Ошибка инициализации\n" + ex.Message); } } public MySqlWrapper(string hostname, string database, string user, string password, string port) :this(string.Format("Server={0};Port={1};Database={2};Uid={3};Pwd={4};",hostname,port,database,user,password)) { } public MySqlResult ExecuteScalar(string sql) { MySqlResult result = new MySqlResult(); result.Error = true; try { this.command = new MySqlCommand(sql, this.connection); this.command.CommandTimeout = this.executionTimeOut; result.Result = this.command.ExecuteScalar().ToString(); result.Error = false; return result; } catch (Exception ex) { this.ErrorLog.Add("Ошибка выполнения скалярного запроса\n" + ex.Message); return result; } } public MySqlResult ExecuteNonQuery(string sql) { MySqlResult result = new MySqlResult(); result.Error = true; try { this.command = new MySqlCommand(sql, this.connection); this.command.CommandTimeout = this.executionTimeOut; this.command.ExecuteNonQuery(); result.Error = false; return result; } catch (Exception ex) { this.ErrorLog.Add("Ошибка выполнения запроса\n" + ex.Message); return result; } } public MySqlDataResult ExecuteReader(string sql) { MySqlDataResult result = new MySqlDataResult(); result.Error = true; try { MySql.Data.MySqlClient.MySqlDataAdapter mySqlAdapter = new MySql.Data.MySqlClient.MySqlDataAdapter(); this.command = new MySqlCommand(sql, this.connection); mySqlAdapter.SelectCommand = this.command; DataSet dst = new DataSet(); mySqlAdapter.Fill(dst); result.ResultDataTable = dst.Tables[0]; result.Error = false; return result; } catch (Exception ex) { this.ErrorLog.Add("Ошибка выполнения запроса\n" + ex.Message); return result; } } public void SetExecutionTimeOut(int value) { this.executionTimeOut = value; } public bool OpenConnection() { try { this.connection.Open(); return true; } catch (Exception ex) { this.ErrorLog.Add("Ошибка соединения\n" + ex.Message); return false; } } public void CloseConnection() { this.connection.Close(); } } public class MySqlResult { public string Result; public bool Error; } public class MySqlDataResult { public DataTable ResultDataTable; public bool Error; } }
UPD
Выкладываю коннектор для .NET, который использовался MySQLConnector .NET v6.2.4.0
Какой коннектор использовали?
Использовал стандартный с mysql.com, сейчас уже не помню, но вроде как последней версии конектор у меня не заработал, а вот предпоследней все было окей. Выложил рабочий тот что использовал.