Code:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using Microsoft.Data.Odbc; namespace [NAMESPACE] { class ODBC { private OdbcConnection Con; private OdbcCommand ConDS; public OdbcDataReader ConDR = null; public bool Connect(String IP, String Password, String Database) { return Connect(IP, "3306", "root", Password, Database); } public bool Connect(String IP, String Username, String Password, String Database) { return Connect(IP, "3306", Username, Password, Database); } public bool Connect(String IP, String Port, String User, String Password, String Database) { try { Con = new OdbcConnection(@"Driver={MySQL ODBC 5.1 Driver};Server=" + IP + ";Port=" + Port + ";User=" + User + ";Password=" + Password + ";Database=" + Database + ";Option=3"); Con.Open(); } catch { Con.Close(); return false; } return true; } public void Query(String SQL) { Query(SQL, false); } public void Query(String SQL, Boolean Read) { if (Con.State == ConnectionState.Open) { ConDS = new OdbcCommand(SQL, Con); if (Read == true) { if (ConDR != null && ConDR.IsClosed.Equals(false)) { ConDR.Close(); } ConDR = ConDS.ExecuteReader(); } } } public string Result(int Column) { if (ConDR != null && ConDR.IsClosed.Equals(false)) { try { if (ConDR.Read()) { return ConDR.GetValue(Column).ToString(); } } catch { return null; } } return null; } public void FreeResult() { if (ConDR != null && ConDR.IsClosed.Equals(false)) { ConDR.Close(); ConDR = null; } } } }
It's tested and works great!
Here is an example;
Code:if (MySQL.Connect("localhost", "username", "password", "database")) { MySQL.Query("SELECT * FROM `test` WHERE `id` = '1';", true); String Result = MySQL.Result(1); if (Result != null) { MessageBox.Show(Result); } MySQL.FreeResult(); }
http://forum.logicalgamers.com/tutor...onnection.html
Results 1 to 3 of 3
Thread: VC# 2008 Express - MySQL Class
- 29 Jan. 2010 05:44am #1
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00
VC# 2008 Express - MySQL Class
- 29 Jan. 2010 08:08pm #2
- 29 Jan. 2010 09:13pm #3
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 100.00