Skills. NET how to access the MySQL database

Source: Internet
Author: User
Tags character set odbc mysql reference table name mysql database
mysql| Access | skills | data | database. NET database natural support MSSQLSERVER, but not other databases do not support, but Microsoft based on their own interests need to push their own database products in support, marketing, but as a platform strategy, he is not to exclude other databases, but reference to the Java system to put forward a set of database access specifications, Let each third party develop and provide a specific driver.

MySQL is a free database, with an irreplaceable advantage in terms of cost, but for the moment it is not available. Microsoft to use MySQL as an ODBC database, you can follow the Odbc.net specification for access, specific reference

Http://www.microsoft.com/china/community/Columns/Luyan/6.mspx

And in fact, for ODBC. NET needs to configure DSN trouble, but there is an open source system Mysqldrivercs, the development of MySQL is encapsulated to achieve the. NET environment for MySQL database system access.

http://sourceforge.net/projects/mysqldrivercs/

By reading the source code, we see Mysqldrivercs's idea is to use the C function of the underlying library to manipulate the database, usually provide access to the MySQL database of the database of C A DLL is a driver file named LibmySQL.dll, Mysqldrivercs encapsulates a C-style drive as a. NET Library.

How exactly is this going to work?

After opening the project, we see that there is a special. cs file CPrototypes.cs:

The following is a reference fragment:

#region LICENSE
/*
Mysqldrivercs:an C # driver for MySQL.
Copyright (c) 2002 Manuel Lucas VI 馻 s Livschitz.

This is part of Mysqldrivercs.

Mysqldrivercs is free software; You can redistribute it and/or modify
It under the terms of the GNU general public License as published by
The free Software Foundation; Either version 2 of the License, or
(at your option) any later version.

Mysqldrivercs is distributed into the hope that it would be useful,
but without any WARRANTY; Without even the implied warranty of
merchantability or FITNESS for A particular purpose. The
The GNU general public License for the more details.

You are should have received a copy of the GNU general public License
Along with Mysqldrivercs; If not, write to the free Software
Foundation, Inc., Temple Place, Suite, Boston, MA 02111-1307 USA
*/
#endregion
Using System;
Using System.Data;
Using System.Runtime.InteropServices;
Namespace Mysqldrivercs
{

[StructLayout (LayoutKind.Sequential)]
public class Mysql_field_factory
{
static string version;
public static Imysql_field getinstance ()
{

if (version==null)
{
Version = Cprototypes.getclientinfo ();
}
if (Version.compareto ("4.1.2-alpha") >=0)
{
return new Mysql_field_version_5 ();
}
Else
return new Mysql_field_version_3 ();
}
}
public interface Imysql_field
{
String Name{get;}
UINT Type{get;}
Long max_length {get;}
}
<summary>
Field Descriptor
</summary>
[StructLayout (layoutkind.sequential)]//"3.23.32", 4.0.1-alpha
Internal class Mysql_field_version_3:imysql_field
{
<summary>
Name of column
</summary>
public string name;
<summary>
Table of column If column was a field
</summary>
public string table;
public string org_table; /* ORG table name if Table is an alias */
public string db; /* Database for Table */
<summary>
Def
</summary>
public string def;
<summary>
Length
</summary>
public long length;
<summary>
Max_length
</summary>
public long max_length;
<summary>
DIV Flags
</summary>
public UINT Flags;
<summary>
Number of decimals in field
</summary>
public UINT Decimals;
<summary>
Type of field. Se mysql_com.h for types
</summary>
public UINT type;

<summary>
Name
</summary>
public string Name
{
Get{return name;}
}
<summary>
Type
</summary>
Public UINT Type
{
Get{return type;}
}
<summary>
Max_length
</summary>
Public long Max_length
{
get {return max_length;}
}
}

<summary>
Field Descriptor
</summary>
[StructLayout (LayoutKind.Sequential)]
Internal class Mysql_field_version_5:imysql_field
{
<summary>
Name of column
</summary>
public string name;
<summary>
Original column name, if an alias
</summary>
public string org_name;
<summary>
Table of column If column was a field
</summary>
public string table;
<summary>
ORG Table name if Table is an alias
</summary>
public string org_table;
<summary>
Database for table
</summary>
public string db;
<summary>
Catalog for table
</summary>
public string Catalog;
<summary>
Def
</summary>
public string def;
<summary>
Length
</summary>
public long length;
<summary>
Max_length
</summary>
public long max_length;
<summary>
Name_length
</summary>
public UINT Name_length;
<summary>
Org_name_length
</summary>
public UINT Org_name_length;
<summary>
Table_length
</summary>
public UINT Table_length;
<summary>
Org_table_length
</summary>
public UINT Org_table_length;
<summary>
Db_length
</summary>
public UINT Db_length;
<summary>
Catalog_length
</summary>
public UINT Catalog_length;
<summary>
Def_length
</summary>
public UINT Def_length;
<summary>
DIV Flags
</summary>
public UINT Flags;
<summary>
Number of decimals in field
</summary>
public UINT Decimals;
<summary>
Character Set
</summary>
public UINT Charsetnr;
<summary>
Type of field. Se mysql_com.h for types
</summary>
public UINT type;

<summary>
Name
</summary>
public string Name
{
get {return name;}
}
<summary>
Type
</summary>
Public UINT Type
{
get {return type;}
}
<summary>
Max_length
</summary>
Public long Max_length
{
get {return max_length;}
}
}
[StructLayout (LAYOUTKIND.EXPLICIT)]
public enum Enum_field_types
{
Field_type_decimal, Field_type_tiny,
Field_type_short, Field_type_long,
Field_type_float, Field_type_double,
Field_type_null, Field_type_timestamp,
Field_type_longlong,field_type_int24,
Field_type_date, Field_type_time,
Field_type_datetime, Field_type_year,
Field_type_newdate,
field_type_enum=247,
field_type_set=248,
field_type_tiny_blob=249,
FIELD_TYPE_MEDIUM_BLOB=250,
field_type_long_blob=251,
field_type_blob=252,
field_type_var_string=253,
field_type_string=254,
field_type_geometry=255

};

<summary>
C prototypes Warpper for Mysqllib.
</summary>
Internal class Cprototypes
{
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_init")]
Unsafe public static extern void* Mysql_init (void* must_be_null);
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_close")]
Unsafe public static extern void Mysql_close (void* handle);

BEGIN addition 2004-07-01 by Alex Seewald
Enables us to call mysql_option to activate compression and timeout
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_options")]
Unsafe public static extern void Mysql_options (void* mysql, uint option, uint *value);
End addition 2004-07-01 by Alex Seewald
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_real_connect")]
Unsafe public static extern void* Mysql_real_connect (void* mysql, string host, String user, string passwd, string db, uint Port, string unix_socket, int client_flag);
[DllImport ("LibmySQL.dll", entrypoint= "mysql_query")]
unsafe public static extern int mysql_query (void*mysql, string query);
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_store_result")]
Unsafe public static extern void *mysql_store_result (void *mysql);
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_free_result")]
Unsafe public static extern void Mysql_free_result (Void*result);
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_errno")]
Unsafe public static extern uint Mysql_errno (VOID*MYSQL);
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_error")]
Unsafe public static extern string mysql_error (Void*mysql);
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_field_count")]
Unsafe public static extern uint Mysql_field_count (VOID*MYSQL);
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_affected_rows")]
Unsafe public static extern ulong Mysql_affected_rows (void*mysql);
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_num_fields")]
Unsafe public static extern uint Mysql_num_fields (Void*result);
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_num_rows")]
Unsafe public static extern ulong mysql_num_rows (void *result);
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_fetch_field_direct")]
Unsafe public static extern IntPtr Mysql_fetch_field_direct (void*result, uint FIELDNR);

<returns> returns a String that represents the client library version </returns>
[DllImport ("LibmySQL.dll", Charset=system.runtime.interopservices.charset.ansi,
Entrypoint= "Mysql_get_client_info", Exactspelling=true)]
public static extern string Getclientinfo ();

[DllImport ("LibmySQL.dll", entrypoint= "Mysql_fetch_row")]
Unsafe public static extern IntPtr mysql_fetch_row (Void*result);
[DllImport ("LibmySQL.dll", entrypoint= "mysql_select_db")]
unsafe public static extern int mysql_select_db (void*mysql,string dbname);
[DllImport ("LibmySQL.dll", entrypoint= "Mysql_fetch_lengths")]
Unsafe public static extern UInt32 *mysql_fetch_lengths (Void*result);

}
}

Basically, the C-style base data structure is redefined by. NET and then accessed through InteropServices.

How to use this library to do operations, you can refer to the examples.

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.