Using the MySQL database in a C # program

Source: Internet
Author: User
Tags mysql database visual studio
mysql| Program | data | Database to mention the MySQL database, as if to say java,jsp or PHP, in fact, in all programs can use the MySQL database, other programs using my SQL database method is to use my SQL provided ODBC driver, in. Net You can use something similar to JDBC. Its name is: MySQLDriverCS.dll. Add it to the. NET palette and how to add it is not the task of this article (^_^)!

Here is a link to create a database:

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 your code and then enter a "." You can see all the things in the Mysqldrivercs namespace.

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

Here is insert:

String value = "Value";
int settingid = 1;


New Mysqlinsertcommand (Conn,
New object[,] {{"Settingid", settingid},{"Settingvalue", Value}},
"Mysqldrivercs_test"
);

Here's the update.

Value = "Value2";
New Mysqlupdatecommand (Conn,
New object[,] {{"Settingvalue", Value}},
"Mysqldrivercs_test",
New object[,] {{"Settingid", "=", Settingid}},
Null
);

The following is a 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 ();


Here's the delete.
New Mysqldeletecommand (conn, "Mysqldrivercs_test", new object[,] {{"Settingid", "=", settingid}},null);

Close Link:

Conn. Close ();


How, very convenient!



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.