Use the mysql database in the C # program

xiaoxiao2021-03-06  161

When it comes to the MySQL database, it is just that Java, JSP or PHP, actually using the mysql database in all programs, other programs use my SQL database method to use the ODBC driver provided by My SQL, can be used in .NET Similar to the JDBC's stuff. Its name is: mysqldrivercs.dll. Add it to the .NET component panel, as for how to add, it is not the task of this article (^ _ ^)!

Here is to create a database link:

Using mysqldriver;

MySQLConnection conn = new mysqlconnection (New MySQLConnectionstring ("localhost", "test", "root", ""). Asstring;

Cn.open ();

If you use the .NET's integrated development environment (Visual Studio.net), enter Mysqldriver in the code "." You can see all 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) " " 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 is Update

Value = "value2"; new mysqlupdateCommand (conn, new object [,] {{"settingValue", value}}, "mysqldrivercs_test", new object [,] {{"settingID", "=", settingid}}, null );

Here is SELECT

DataTable dt = new mysqlselectcommand (conn, new string "{" settingid "}, new string [] {" mysqldrivercs_test "}, new object [,] {{" settingID "," = ", settingid}} , NULL, NULL) .TABLE; STRING StoredValue = Dt.Rows [0] ["settingValue"]. TOSTRING (); below is delete new mysqldeleteCommand (CONN, "mysqldrivercs_test", new object [,] {{"settingID", "=", SettingId}}, null;

Close link:

CONN.CLOSE ();

How is it, it is very convenient!

(www.sinoprise.com)

转载请注明原文地址:https://www.9cbs.com/read-127956.html

New Post(0)