About multiple connection MySQL data methods in C #

Source: Internet
Author: User
Tags odbc mysql tutorial msdr

About multiple connections in C # MySQL tutorial data method
Method One:
Use MySQL to launch the MySQL connector/net is a ado.net driver for MySQL
This component is a. NET access component designed for MySQL to access the MySQL database tutorial for ado.net.
After installation completes the component, the namespace mysql.data.mysqlclient is referenced;
When compiling using the command line: Csc/r:mysql.data.dll Test.cs
Method Two:
Access to the MySQL database through ODBC
Download two components before accessing: ODBC Driver for Odbc.net and MySQL (MySQL connector/odbc (MYODBC) driver) is currently version 3.51
After the installation is complete, you can access the MySQL database via ODBC
Method Three:
Use the MySQL access component launched by Corelab to target. Net
After the installation is complete, reference namespaces: Corelab.mysql;
Compile with command: Csc/r:corelab.mysql.dll Test.cs

Mysqlconnection conn = null;
Try
{
Mysqlconnection conn = new Mysqlconnection (New mysqlconnectionstring ("Myquest", "root", "Amttgroup"). asstring);
Instantiate a Connection object where Myquest is the database name, root is the database user name, Amttgroup is the database password
String connstr = "Data Source=mysql;password=root;user id=root;location=localhost";
conn = new mysqlconnection (CONSTR);
Conn.Open ();
string query = "INSERT into test.dbtable values (ten, ' Disksidkfsdi ', ' asdfaf ', ' adsfasdf ')";
string tmp = NULL;
Mysqlcommand cmd = new Mysqlcommand (query, conn);
for (int i = 0; i < 100000; i++)
{
Cmd.executenonquery ();
}
Cmd.dispose ();
Conn.close ();
query = "SELECT * from test.dbtable";
Mysqlcommand cmd2 = new Mysqlcommand (query, conn);
Conn.Open ();

Set Chinese output
Mysqlcommand firstcmd = new Mysqlcommand ("Set names gb2312", myconn);
Firstcmd.executenonquery ();

Mysqldatareader reader = Cmd2.executereaderex ();
while (Reader.read ())
{
TMP = Reader[0].tostring ();
TMP = Reader[1].tostring ();
TMP = Reader[2].tostring ();
TMP = Reader[3].tostring ();
}
Conn.close ();
query = "Delete from test.dbtable";
Mysqlcommand cmd3 = new Mysqlcommand (query, conn);
Conn.Open ();
Cmd3.executenonquery ();
}
catch (Exception ex)
{
MessageBox.Show (Ex.message);
}
Finally
{
Conn.close ();
}


MYSQLDRIVERCS implementation accesses the MySQL database in the. NET Environment

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

}

}

This enables access to MySQL data in the. NET environment. Before that, you have to do some preparation, download Mysqldrivercs, install, find Mysqldriver.dll under the installation folder, and then add Mysqldriver.dll references to the project OK. Maybe you're right about this line of code

Mysqlcommand commn = new Mysqlcommand ("Set names gb2312", conn);

Commn.executenonquery ();


Methods provided by netizens

The following is an example of accessing a MySQL database

compiler directive: Csc/r:corelab.mysql.dll/r:mysql.data.dll Test.cs

using System;
Using System.Net;
Using System.Text;
Using Corelab.mysql;
Using System.Data.Odbc;
Using Mysql.data.mysqlclient;

Class Connectmysql
{

public void Connect_corelab ()
{
String constr = "User id=root;host=localhost;database=qing;password=qing";
Mysqlconnection mycn = new Mysqlconnection (CONSTR);
Mycn.open ();
Mysqlcommand mycm = new Mysqlcommand ("SELECT * from Shop", MYCN);
Mysqldatareader MSDR = Mycm.executereader ();
while (Msdr.read ())
{
if (msdr.hasrows)
{
Console.WriteLine (msdr.getstring (0));
}
}
Msdr.close ();
Mycn.close ();
}

public void Connect_odbc ()
{
String myconstring = "Dsn=mysql;uid=root;pwd=qing";
String myconstring = "Driver={mysql ODBC 3.51 driver};"
"Server=localhost;" +
"Database=test;" +
"Uid=root;" +
"Password=qing;" +
"Option=3";
OdbcConnection myconn = new OdbcConnection (myconstring);
MyConn.Open ();
OdbcCommand mycm = New OdbcCommand ("SELECT * from Hello", myconn);
OdbcDataReader MSDR = Mycm.executereader ();
while (Msdr.read ())
{
if (msdr.hasrows)
{
Console.WriteLine (msdr.getstring (0));
}
}
Msdr.close ();
Myconn.close ();
}
public void Connect_net ()
{
String myconnectionstring = "Database=test;data source=localhost;user id=root;password=qing";
Mysqlconnection mycn = new Mysqlconnection (myconnectionstring);
Mycn.open ();
Mysqlcommand mycm = new Mysqlcommand ("SELECT * from Hello", mycn);
Mysqldatareader MSDR = Mycm.executereader ();
while (Msdr.read ())
{
if (msdr.hasrows)
{
Console.WriteLine (msdr.getstring (0));
}
}
Msdr.close ();
Mycn.close ();
}
public static void Main ()
{
Connectmysql ms = new Connectmysql ();
Ms.connect_corelab ();
Ms.connect_odbc ();
Connect_net ();
}
}

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.