C # Two ways to connect to the MySQL database

Source: Internet
Author: User
Tags mysql tutorial

C # Two ways to connect to the MySQL Tutorial database tutorial

MySQL provides a DLL file-"Mysql.data.dll", now I use this DLL file, the file (see Attachment) in the Web site Bin directory, you can use. and the use of SQL Server is not much different, but the application of the name is not the same.
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 Headlines" ></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.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);
}
}
}

Method Two

Locate Mysqldriver.dll under the installation folder, and then add Mysqldriver.dll to the project

Note: I downloaded the version is 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.executenonquery ();

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 ();


}

}

}

Related components can be downloaded to 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.