C # Two Methods for connecting to the mysql database

Source: Internet
Author: User
Tags mysql tutorial

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.