C # how to connect to MySQL

Source: Internet
Author: User

1. From http://prdownloads.sourceforge.net/mysqldrivercs/MySQLDriverCS-n-EasyQueryTools-3.0.18.exe? Download mysqldrivercs.
2. Add mysqldrivercs. dll. To the. NET Component
3. Usage
Create a database link as follows:
Create a database link as follows:
Using mysqldrivercs;
 
Mysqlconnection conn = new mysqlconnection (New mysqlconnectionstring ("localhost", "test", "root", ""). asstring );
Conn. open ();
 
If you are using the. NET integrated development environment (visual studio.net), enter mysqldrivercs in the code and then "." To see everything in the mysqldrivercs namespace.
Below is commond:
Mysqlcommand cmd;
Cmd = new mysqldrivercs. mysqlcommand ("Drop table if exists test. mysqldrivercs_test", Conn );
Cmd. executenonquery ();
Cmd. Dispose ();
 
Cmd = new mysqldrivercs. mysqlcommand ("create table test. mysqldrivercs_test (" +
"Settingid tinyint (3) unsigned not null auto_increment," +
"Settingvalue text," +
"Primary Key (settingid), unique key settingid (settingid), key settingid_2 (settingid)" +
"Type = MyISAM comment = 'mysql test table'", Conn );

Cmd. executenonquery ();
Cmd. Dispose ();
Insert:
Method prototype:
Public mysqlinsertcommand (
Mysqlconnection Conn,
Object [,] fieldsandvalues,
String table
);
String value = "value ";
Int settingid = 1;

New mysqlinsertcommand (Conn,
New object [,] {"field1", settingid}, {"field2", value }},
"Mysqldrivercs_test"
);
Mysqldrivercs_test is the table name.
 
 
Below is the update
Method prototype:
Public mysqlupdatecommand (
Mysqlconnection Conn,
Object [,] fieldsandvalues,
String table,
Object [,] whereparamfields,
String [,] wherenoparamfields
);
 
 
Value = "value2 ";
New mysqlupdatecommand (Conn,
New object [,] {"settingvalue", value }},
"Mysqldrivercs_test ",
New object [,] {"settingid", "=", settingid }},
Null
);
Select
Datatable dt = new mysqlselectcommand (Conn,
New String [] {"settingid", "settingvalue "},
New String [] {"mysqldrivercs_test "},
New object [,] {"settingid", "=", settingid }},
Null,
Null
). Table;
String storedvalue = DT. Rows [0] ["settingvalue"]. tostring ();

Below is the delete
New mysqldeletecommand (Conn, "mysqldrivercs_test", new object [,] {"settingid", "=", settingid}, null );
Close link:
Conn. Close ();

 

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.