This article will connect to the MySQL database in two ways. There may be many connection methods, but here we only use two of them: One is written by a third party: bytefx, and the other is completed through odbcconnection. First, make sure that your machine has installed MySQL and runs it correctly.
Bytefx. Data. mysqlclient; ... ... ...
Myconnectionstring = "DSN = MySQL; uid = root; Pwd = root "; Mysqlconnection myconnection = new mysqlconnection (myconnectionstring ); String myinsertquery = "select * from authors "; Mysqlcommand mycommand = new mysqlcommand (myinsertquery ); Mycommand. Connection = myconnection; Myconnection. open (); Mysqldatareader objreader = mycommand. executereader (); Int nresultcount = 0; While (objreader. Read ()) { ++ Nresultcount; } Another method is to use ODBC:
Using system. Data. ODBC; ... ... String myconnectionstring = "DSN = MySQL; uid = root; Pwd = root "; Odbcconnection myconn; Odbccommand mycmd = new odbccommand (); Myconn = new odbcconnection (myconnectionstring ); Myconn. open (); Mycmd. Connection = myconn; Stringbuilder SQL = new stringbuilder (); SQL. append ("select "); SQL. append ("*"); SQL. append ("from "); SQL. append ("Authors "); Mycmd. commandtext = SQL. tostring (); Odbcdatareader result = mycmd. executereader (commandbehavior. closeconnection ); Int nresultcount = 0; While (result. Read ()) { ++ Nresultcount; } |