While I was working on some projects I needed an easy abstract access to the data that was in the MySQL database. So I wrote this class, to be used where I felt like to.
Of course it needs a mysql .NET connector (driver).
You can get the full Visual Studio zipped project archive right here.
You can use the code in your program like this:
test1.cs
using System;
//using System.Collections.Generic;
using System.Collections;
//using System.Linq;
using System.Text;
namespace Project1
{
class test1
{
static void Main(string[] args)
{
// change this of course
MySQLConnection sqlserver = new MySQLConnection("server=DBADDRESS; user id=DBUSER; database=TESTDATABASE; password=DBPASS; ", true);
// and this...
string table = "`TESTDATABASE`.`_testtable`";
sqlserver.SqlExecute("CREATE TABLE IF NOT EXISTS " +
table + " ( " +
"`id` INT NOT NULL AUTO_INCREMENT, " +
"`stuff` VARCHAR( 256 ) NOT NULL, " +
"PRIMARY KEY ( `id` ) " +
") ENGINE = MYISAM", null);
sqlserver.SqlExecute("TRUNCATE " + table, null);
sqlserver.SqlExecute("INSERT INTO " + table +
" (`id` ,`stuff`)" +
"VALUES (NULL , 'aa'), (NULL , 'bb') ", null);
Hashtable sql_param = new Hashtable();
sql_param.Add("stuff1", "cc");
sql_param.Add("stuff2", "dd");
sqlserver.SqlExecute("INSERT INTO " + table +
" (`id` ,`stuff`)" +
"VALUES (NULL , @stuff1), (NULL , @stuff2) ",
sql_param); // No quotes!
ArrayList somedata = new ArrayList();
somedata = sqlserver.SqlFetchList("SELECT stuff FROM " +
table, null);
foreach (string d in somedata)
{
Console.WriteLine("'" + d.ToString() + "'");
}
}
}
}