Use MySQL. Data. mysqlclient to connect to the MySQL database.

Source: Internet
Author: User

I have been developing in my company for more than two years. A friend asked me a few days ago if I could use it. net to connect to the MySQL database, there were some issues at that time, but I had a good review and found that so many projects
ASP/ASP. NET and SQL
Server database, and Java is also used with MySQL/Oracle databases in most cases, as if the soy juice must be configured with a coke ring. Suddenly use. net connection
I think it is not so comfortable to connect to MySQL. However, the question raised by a friend is indeed a challenge. Although the. Net control can be used to solve the problem for a friend
MySQL ODBC can be created on the server. If the website is migrated to another server, you need to re-establish the data source, which is very uncomfortable. Helpless, Google a bit on Baidu, found that the original
MySQL had previously thought about the connection to. NET and provided a solution, which is much more generous than Microsoft.
MySQL provides a DLL file-"mysql. Data. dll", currently I am using this DLL file, will this file (: http://download.csdn.net/source/1497487
In the bin directory of the website. In addition, it is not much different from SQL server usage, but the application name is different.
The Code is as follows:
1. default. aspx:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<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>
</Body>
</Html>

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. htmlcontrols;
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 = mysqlcmd. executereader ();
While (mysqlrdr. Read ())
{
Tablerow TR = new tablerow ();
Tablecell Tc = new tablecell ();
TC. Text = mysqlrdr ["name"]. tostring ();
Tr. cells. Add (TC );
Tbltest. Rows. Add (TR );
}
}
}

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.