In the previous article http://www.jb51.net/article/61219.htm said in C # with the official MySQL driver how to connect to operate the MySQL driver, MySQL is the JDBC driver has two, the MySQL driver for. NET is also There are two of them. This is the second driver of. NET connection MySQL, from Sourceforge's open source driver.
First of all, to http://sourceforge.net/projects/mysqldrivercs/download, write this article when the version is Mysqldrivercs-n-easyquerytools-4.0.1-dotnet2.0.exe. installation, with the source code and examples, only three dynamic libraries Libmysql-4.0.dll, LibmySQL.dll, MySQLDriverCS.dll, light look at the dynamic library is not as powerful as the official version of the drive. You can start by introducing MySQLDriverCS.dll from the installation directory into your C # project. Use the Help\sdk\documentation.chm Help documentation in the installation directory at all times.
It also revolves around a few questions:
Basic usage, such as the notation of the connection string, the basic query operation, which classes will probably be used
Handling of exceptions
The handling of Things
Parameterized query Support
This time I will be in an example of the above four aspects, the following piece of code is a bit confusing, please read it when using, and then to picking:
The code is as follows:
Using System;
Using System.IO;
Using Mysqldrivercs; The namespaces introduced here are the
Using System.Data.Common;
Namespace Cc.unmi
{
public class Program
{
public static void Main (string[] args)
{
Mysqldrivercs has Mysqlconnectionstringbuilder and mysqlconnectionstring to construct the connection string.
In fact, it's a bit of a trick, maybe used to directly use the connection string
mysqlconnectionstring constr = new mysqlconnectionstring ("localhost", "unmi_db", "Unmi", "xxxxxx");
From Mysqlconnectionstringbuilder It seems that there are fewer properties to support, and I don't know how the connection pool functions.
String connstr = "Data source=unmi_db; password=xxxxxx; User Id=unmi; Location=localhost; port=3306; Extended properties= ";
Mysqlconnection conn = new mysqlconnection (constr.asstring); or new Mysqlconnection (CONNSTR);
Conn. Open ();
Parameters with @, same support? The way, should also be not recommended? The form
String sql = "Update wp_options set option_value= ' http://unmi.cc ' where option_id=?id and <a href=" Mailto:[email prot Ected] ">[email protected]</a>";
String sql = "Select option_name from Wp_options where Option_id=?id and <a href=" Mailto:[email protected] ">[email p Rotected]</a> ";
Mysqlcommand com = new Mysqlcommand (SQL, conn);
Mysqlparameter paraid = new Mysqlparameter ("? id", 1);
Mysqlparameter paraname = new Mysqlparameter ("@name", "SiteURL");
In fact, it was added to the mysqlparametercollection.
It seems that AddWithValue () has the same work as the other, for example, it can be written as com. Parameters.Add (New Mysqlparameter ("@id", 1);
Com. Parameters.Add (Paraid);
Com. Parameters.Add (Paraname);
If the query
Mysqldatareader dr = com. Executereaderex ();//execute Executereaderex () the Mysqldatareader is returned.
Dbtransaction trans = conn. BeginTransaction (); Open Things
Try
{
Com. ExecuteNonQuery ();
You should also use things when you want to execute more statements.
Console.WriteLine (COM. ExecuteScalar (). ToString ());
Trans.commit ();
}
catch (Mysqlexception ex)
{
Console.WriteLine (ex. Message);
Trans. Rollback ();
}
Finally
{
Conn. Close ();
}
Console.readkey ();
}
}
}
Although the driver file name has DotNet2.0 words, it should be as long as the 2.0 and above the. NET runtime, I am in 32-bit XP, Visual Studio 2008, with. Net 3.5来 Run, no problem.
Note that in my home computer is not running up, the hint is:
The code is as follows:
{"Could not load file or assembly ' Mysqldrivercs, version=3.0.1735.36021, Culture=neutral, publickeytoken= 172f94dfb0faf263 ' or one of its dependencies. An attempt is made to load a program with an incorrect format. "}
It's my hateful 64-bit XP, it can only support 32-bit applications, and the official version of the driver performance is not the same. The official version of the MSI program cannot be installed under my 64-bit system, but the. dll in the installation package can be used under 64-bit. And this version of the driver is able to install, but use it to tell me no, obviously dumped me. In fact, on the download-driven Web page, there are 32 of the various winows operating systems are noted.
Finally, there is a little difference between the driver and the official version, the connection string is not rich in the official version of the driver, and do not know how to support the connection pool. When setting parameters with parameterized queries, it is less convenient than the AddWithValue () method used by the official version, although it is easier than the ADO of the label. And then in the 64 bit can not run at all, this could not be considered a cause injury, the server under the 64-bit system is unavoidable.
However, this open-source driver is also somewhat unique in that it provides Mysqlinsertcommand, Mysqlselectcommand, Mysqlupdatecommand, and Mysqldeletecommand classes that facilitate data manipulation, You do not need to explicitly write SQL statements, much like the corresponding functions of WordPress $wpdb: $wpdb->insert (), $wpdb->query (), $wpdb->update () and $wpdb Delete ().
This should not have been driven so distinct characteristics of the following. Take a look at the operation of these several functions:
The code is as follows:
New Mysqlinsertcommand (
dbcon,//Connection
New object[,] {//Field name and corresponding value
{"Settingid", 100},
{"Settingvalue", "Http://www.jb51.net"}
},
"Settings"//Table name
); When new is executed, the available bsuccess and Query properties are respectively successful or not and the corresponding SQL
The Mysqlselectcommand is a bit of a pain in the egg, it's better to direct SQL.
DataTable dt = new Mysqlselectcommand (Dbcon,//connection
New string[] {"Settingid", "Settingvalue"},//the list of fields to query
New string[] {"Settings"},//table to query, can be multiple
New object[,] {{"Settingid", "=", 100}},//condition
Null
Null//can also be followed by limit, distinct and other instructions and parameters
). Table; Get a DataTable
This is a convenient way to update it.
New Mysqlupdatecommand (Dbcon,
New object[,] {{"Settingvalue", "Http://www.jb51.net"}},
"Settings",
New object[,] {{"Settingid", "=", 100}},
Null
); When new is executed, the available bsuccess and Query properties are respectively successful or not and the corresponding SQL
The parameters are the same as the following four Mysqlupdatecommand
New Mysqldeletecommand (Dbcon, "Trash", NULL, NULL);
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # uses an open source driver connection to operate the MySQL database
This address: http://www.paobuke.com/develop/c-develop/pbk23134.html
Related content C #?? Wordxa?? 3épdf? ¨?? Xü (? ùóúofficeoíwps) C#byte array with image convert instance code C # Three implementations of XML reading using C # to implement web crawler
C # custom DataGridViewColumn show treeviewc# to traverse all drives under the operating system C # Tip: Workaround for "Unable to find manifest signing certificate in certificate store" repeater adding a button to implement a method of clicking a button to get a row of data
C # uses an open source driver connection to operate the MySQL database