How does. NET access the MySQL database?

Source: Internet
Author: User
. Net databases support MSSQLServer, but not other databases, but Microsoft pushes its own database products in support and marketing based on its own interests and needs. However, as a platform strategy, instead of rejecting other databases, he proposed a set of database access specifications based on the Java System to allow various third parties to develop and provide specific drivers.

MySQL is a free database with irreplaceable advantages in terms of cost, but it is not provided yet. Microsoft regards MYSQL as an ODBC database and can be accessed according to ODBC. Net specifications. For details, refer

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

In fact, for ODBC. Net requires the trouble to configure DSN, but an open-source system mysqldrivercs has emerged. It encapsulates MySQL development and implements access to the MySQL database system in the. NET environment.

Http://sourceforge.net/projects/mysqldrivercs/

By reading the source code, we can see that the idea of mysqldrivercs is to use the underlying library of the C function to manipulate the database. Generally, the c dll of the database that provides access to the MySQL database is called libmysql. DLL driver file, mysqldrivercs as.. Net Library to encapsulate the C-style driver.

How to proceed?

After opening the project, we can see that there is a special. CS file cprototypes. CS:

The following is a reference clip:

# Region license
/*
Mysqldrivercs: An C # driver for MySQL.
Copyright (c) 2002 Manuel Lucas Vi s Livschitz.

This file 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
The Free Software Foundation; either version 2 of the License, or
(At your option) any later version.

MySQLDriverCS is distributed in the hope that it will be useful,
But without any warranty; without even the implied warranty
MERCHANTABILITY or fitness for a particle PURPOSE. See
GNU General Public License for more details.

You shoshould have your ed a copy of the GNU General Public License
Along with MySQLDriverCS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, 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 was 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 was 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 basic data structure is redefined by. NET and accessed through interopservices.

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.