most of the people think that is it possible to connect C# with MySql, and keep on thinking that how can i do it ???? Even after having the MySql driver for C# it is a little difficult for a simple C# user to connect it to MySql. Even when i have downloaded the driver myself it was a difficult task for me to go for it. So i made lots of sample project to basically connect to MySql through C#, After having these all experience, i designed a simple class just like SqlClient and basic functions to get connected to MySql on any host and get work done easily
It is very easy to use this class even u will see that the functions are self explanatory
U just simple have to know to which host are u connecting, port number and the schema ur selecting and start executing queries the way u want
isn’t that simple
i will also improve this class later when i will have a response that people are happy with this class and yes this class is open source so u may change the way u want it to
or just use its default
You may check it ur self, some code is shown below the rest i guess u can handle
Explanation :
for this class to run properly u need some pre-requisites :
Mysql, A database, Mysql Connection Driver For .Net (Easily available on www.mysql.com)
after all the pre-requisites you need a project, on that project u need to add the reference MySQL.Data from add reference in ur project and now u may even add reference for MySQL.Web so as u wish but MySQL.Data is necessary for this class and then u need to write on top of ur project where the using block is another pair of lines, i.e.
using MySQLWrap; // as this is the namespace for my wrapper class
then u need a new DatabaseConnection Object that u can do by
DatabaseConnection anObj = new DatabaseConnection(“localhost”, “root”, “”, “mysql”);
in the above line the class constructor is called where localhost is the hostname for the server, root is the username to have access, string.Empty is representing the password for the server and mysql is the schema to select for this connection
now a test query should be run
but before that lets connect first and check if the connection is made correctly or not
if (anObj.connect() == true)
{
//do what u want to do from database.
}
else
{
// there is some problem with the database.
}
now for the query part
if (anObj.executeQuery(“SELECT * FROM table”) == true)
{
// manipulate the data by read() and getRow() functions like
while (anObj.read() == true)
{
Hashtable aRow = anObj.getRow();
aRow["col1"]; // this will give u the desired value in the row for column named col1
}
}
else
{
// there may be some error with ur query :S
}
mysqlEscapeString(anyVariableOfTypeString);
the above function is also provided with the class to get rid of SQL injections in the database
i think this overview is ok for the startup rest u people can comment and i will improve the class by ur response
u can download the file from here : Database Connection.doc
waiting for everyones response and thankyou for ur time
Nice work buddy I’ve seen your class and its really helpful for the people who want to use mysql with C#. Great work! I can expect more nice tips over this forum. Keep posting good stuff.
gr888 work bro (Y) … expecting many more like this from you
nice work man
.hope u will post more good stuff….
waiting for ur next Post:)