C # Two Methods for connecting to the mysql tutorial database tutorial
Mysql provides a dll file named "mysql. data. dll ", I am currently using this dll file, put this file (see the attachment) under the bin directory of the website, you can use it. In addition, it is not much different from SQL server usage, but the application name is different.
The Code is as follows:
1. default. asp tutorial x:
<% @ Page language = "c #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<Form id = "form1" runat = "server">
<Div>
<Asp: table id = "tbltest" runat = "server">
<Asp: tableheaderrow id = "tblrowheader" runat = "server">
<Asp: tableheadercell id = "tblcellheader" runat = "server" text = "news title"> </asp: tableheadercell>
</Asp: tableheaderrow>
</Asp: table>
</Div>
</Form>
2. default. aspx. cs:
Using system. web;
Using system. web. security;
Using system. web. ui;
Using system. web. ui. webcontrols;
Using system. web. ui. webcontrols. webparts;
Using system.web.ui.html controls;
Using mysql. data. mysqlclient;
Public partial class _ default: system. web. ui. page
{
Protected void page_load (object sender, eventargs e)
{
String connstr = configurationmanager. connectionstrings ["connectionstring"]. tostring ();
Mysqlconnection mysqlconn = new mysqlconnection (connstr );
String sqlstr = "select name from t_document order by id desc ";
Mysqlconn. open ();
Mysqlcommand mysqlcmd = new mysqlcommand (sqlstr, mysqlconn );
Mysqldatareader mysqlrdr = mysql20..exe cutereader ();
While (mysqlrdr. read ())
{
Tablerow tr = new tablerow ();
Tablecell tc = new tablecell ();
Tc. text = mysqlrdr ["name"]. tostring ();
Tr. cells. add (tc );
Tbltest. rows. add (tr );
}
}
}
Method 2
Find mysqldriver. dll in the installation folder, and add the mysqldriver. dll to the project.
Note: the version I downloaded is a mysqldrivercs-n-easyquerytools-4.0.1-dotnet2.0.exe
Using system;
Using system. collections. generic;
Using system. componentmodel;
Using system. data;
Using system. data. odbc;
Using system. drawing;
Using system. linq;
Using system. text;
Using system. windows. forms;
Using mysqldrivercs;
Namespace mysql
{
Public partial class form1: form
{
Public form1 ()
{
Initializecomponent ();
}
Private void form1_load (object sender, eventargs e)
{
Mysqlconnection conn = null;
Conn = new mysqlconnection (new mysqlconnectionstring ("localhost", "inv", "root", "831025"). asstring );
Conn. open ();
Mysqlcommand commn = new mysqlcommand ("set names gb2312", conn );
Commn.exe cutenonquery ();
String SQL = "select * from exchange ";
Mysqldataadapter mda = new mysqldataadapter (SQL, conn );
Dataset ds = new dataset ();
Mda. fill (ds, "table1 ");
This. datagrid1.datasource = ds. tables ["table1"];
Conn. close ();
}
}
}
You can download related components from http://dev.mysql.com/downloads/connector/net/5.0.html.