Use mysqldrivercs to access the MySQL database in the. NET Environment

Source: Internet
Author: User
Document directory

Using
System;

Using
System. Data;

Using
System. Data. sqlclient;

Using
System. configuration;

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
Mysqldrivercs;

 

Public
Partial class
_ Default: system. Web. UI. Page

{

Protected void
Page_load (Object sender, eventargs E)

{

Mysqlconnection conn = NULL;

Conn = new mysqlconnection (New
Mysqlconnectionstring ("127.0.0.1", "dvbbs", "root ",
(123456 "). asstring );

Conn. open ();

Mysqlcommand commn = new mysqlcommand ("set names
Gb2312 ", Conn );

Commn. executenonquery ();

String SQL = "select
Title, topicid, boardid
, Dateandtime, hits from dv_topic order by dateandtime
Desc limit 10 ";

Mysqldataadapter MDA = new mysqldataadapter (SQL, Conn );

Dataset DS = new
Dataset ();

MDA. Fill (DS, "Table1 ");

This. repeater1.datasource = Ds;

This. repeater1.databind ();

Conn. Close ();

}

}

In this way, you can access Mysql Data in the. NET environment. Before that, you need to make some preparations. Download mysqldrivercs, install it, find mysqldriver. dll in the installation folder, and add and reference mysqldriver. DLL to the project. Maybe you have code for this line

Mysqlcommand commn = new mysqlcommand ("set names
Gb2312 ", Conn );

Commn. executenonquery ();

I don't quite understand. This is because if there is no such sentence, the data read from the database will be garbled if there is any Chinese character, as long as the statement "set names
"Gb2312" can be executed once as an SQL statement.

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.