zoukankan      html  css  js  c++  java
  • [Jimmy原创] 在.NET环境下操作MySQL数据库 -续

            写完“在.NET环境下操作MySQL数据库”,在和朋友们交流的时候,找到了ByteFX.Data 为什么不能显示中文的原因以及解决之道(感谢Vencent)。

            原来ByteFX.Data默认是依靠MySQL默认字符集(latin1)来编码的,因此显示中文的时候为乱码。可以更改MySQL的默认字符集来解决:在"my.ini"中添加一行default-character-set=gb2312   ,如下图:




    当然如果你在其他的OS下面安装的MySQL,一样可以用相应的指令来设置这个属性。

    之后的结果就OK拉。。。。大家以后可以比较放心的使用这个免费的好冬冬拉


    使用ByteFX.Data 或者说MySQL的官方Connector/Net的一些代码如下:

    using System.Windows.Forms;
    using System.Data;
    using MySql.Data.MySqlClient;




      mySqlConnection2.ConnectionString 
    = "connection timeout=30;server=192.168.8.112;user id=zjmzs; password=zjmzs;
                                                         port=3306; database=popoface;pooling=true; min pool size=3; max pool size=101
    ";

    mySqlConnection2.Open();

    MySqlDataAdapter adapter 
    = new MySqlDataAdapter();

    adapter.SelectCommand 
    = new MySqlCommand("select * from uploadcfd", mySqlConnection2);

    adapter.Fill(dataSet1);

            

            另外在网友Yu 的提示下去http://www.cybercom.net/~zbrad/DotNet/MySql/ 这个地方看了一下他的MySQL解决方法。Brad Merrill这位在微软工作的仁兄主要是写的一个DLL调用MySQL的libmySQL.dll的一个接口,这个也不失为一个不错的方法,不过这个就得针对自己的需要写libmySQL.dll里面的函数的接口,在Brad Merrill提供的mysql.cs里面已经写好了一些比较常用的函数,不过实际使用我还没有去试,大家有兴趣的可以试试看,我们来看看mysql.cs的代码就比较清楚了:

    using System;
    using System.Security;
    using System.Runtime.InteropServices;




      
    ///<summary>Connects to a MySql server</summary>
      
    ///<returns><paramref name="db"/> value on success, else returns IntPtr.Zero</returns>

      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             CharSet
    =System.Runtime.InteropServices.CharSet.Ansi,
             EntryPoint
    ="mysql_real_connect", ExactSpelling=true)]
        
    public static extern IntPtr Connect(IntPtr db,
          [In] 
    string host, [In] string user, [In] string passwd,
          [In] 
    string dbname,
          
    uint port, [In] string socketName, uint flags
          );

      
    ///<summary>Selects a database</summary>
      
    ///<returns>Zero for success.  Non-zero if an error occurred.</returns>

      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             CharSet
    =System.Runtime.InteropServices.CharSet.Ansi,
             EntryPoint
    ="mysql_select_db", ExactSpelling=true)]
        
    public static extern int SelectDb(IntPtr conn, [In] string db);

      
    ///<summary>Closes a server connection</summary>
      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll", EntryPoint="mysql_close", ExactSpelling=true)]
        
    public static extern void Close(IntPtr db);

      
    ///<summary>Executes a SQL query specified as a string</summary>
      
    ///<returns>number of rows changed, -1 if zero</returns>

      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             CharSet
    =System.Runtime.InteropServices.CharSet.Ansi,
             EntryPoint
    ="mysql_query", ExactSpelling=true)]
        
    public static extern int Query(IntPtr conn, [In] string query);

      
    ///<summary>Retrieves a complete result set to the client</summary>
      
    ///<returns>An IntPtr result structure with the results. IntPtr.Zero if an error occurred.</returns>

      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             EntryPoint
    ="mysql_store_result", ExactSpelling=true)]
        
    public static extern IntPtr StoreResult(IntPtr conn);

      
    ///<returns>Returns the number of rows in a result set</returns>
      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             EntryPoint
    ="mysql_num_rows", ExactSpelling=true)]
        
    public static extern int NumRows(IntPtr r);

      
    ///<returns>Returns the number of columns in a result set</returns>
      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             EntryPoint
    ="mysql_num_fields", ExactSpelling=true)]
        
    public static extern int NumFields(IntPtr r);


    using System;
    using System.Security;
    using System.Runtime.InteropServices;




      
    ///<summary>Connects to a MySql server</summary>
      
    ///<returns><paramref name="db"/> value on success, else returns IntPtr.Zero</returns>

      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             CharSet
    =System.Runtime.InteropServices.CharSet.Ansi,
             EntryPoint
    ="mysql_real_connect", ExactSpelling=true)]
        
    public static extern IntPtr Connect(IntPtr db,
          [In] 
    string host, [In] string user, [In] string passwd,
          [In] 
    string dbname,
          
    uint port, [In] string socketName, uint flags
          );

      
    ///<summary>Selects a database</summary>
      
    ///<returns>Zero for success.  Non-zero if an error occurred.</returns>

      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             CharSet
    =System.Runtime.InteropServices.CharSet.Ansi,
             EntryPoint
    ="mysql_select_db", ExactSpelling=true)]
        
    public static extern int SelectDb(IntPtr conn, [In] string db);

      
    ///<summary>Closes a server connection</summary>
      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll", EntryPoint="mysql_close", ExactSpelling=true)]
        
    public static extern void Close(IntPtr db);

      
    ///<summary>Executes a SQL query specified as a string</summary>
      
    ///<returns>number of rows changed, -1 if zero</returns>

      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             CharSet
    =System.Runtime.InteropServices.CharSet.Ansi,
             EntryPoint
    ="mysql_query", ExactSpelling=true)]
        
    public static extern int Query(IntPtr conn, [In] string query);

      
    ///<summary>Retrieves a complete result set to the client</summary>
      
    ///<returns>An IntPtr result structure with the results. IntPtr.Zero if an error occurred.</returns>

      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             EntryPoint
    ="mysql_store_result", ExactSpelling=true)]
        
    public static extern IntPtr StoreResult(IntPtr conn);

      
    ///<returns>Returns the number of rows in a result set</returns>
      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             EntryPoint
    ="mysql_num_rows", ExactSpelling=true)]
        
    public static extern int NumRows(IntPtr r);

      
    ///<returns>Returns the number of columns in a result set</returns>
      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             EntryPoint
    ="mysql_num_fields", ExactSpelling=true)]
        
    public static extern int NumFields(IntPtr r);


    using System.Windows.Forms;
    using System.Data;
    using MySql.Data.MySqlClient;




      mySqlConnection2.ConnectionString 
    = "connection timeout=30;server=192.168.8.112;user id=zjmzs; password=zjmzs;
                                                         port=3306; database=popoface;pooling=true; min pool size=3; max pool size=101
    ";

    mySqlConnection2.Open();

    MySqlDataAdapter adapter 
    = new MySqlDataAdapter();

    adapter.SelectCommand 
    = new MySqlCommand("select * from uploadcfd", mySqlConnection2);

    adapter.Fill(dataSet1);

            

            另外在网友Yu 的提示下去http://www.cybercom.net/~zbrad/DotNet/MySql/ 这个地方看了一下他的MySQL解决方法。Brad Merrill这位在微软工作的仁兄主要是写的一个DLL调用MySQL的libmySQL.dll的一个接口,这个也不失为一个不错的方法,不过这个就得针对自己的需要写libmySQL.dll里面的函数的接口,在Brad Merrill提供的mysql.cs里面已经写好了一些比较常用的函数,不过实际使用我还没有去试,大家有兴趣的可以试试看,我们来看看mysql.cs的代码就比较清楚了:

    using System;
    using System.Security;
    using System.Runtime.InteropServices;




      
    ///<summary>Connects to a MySql server</summary>
      
    ///<returns><paramref name="db"/> value on success, else returns IntPtr.Zero</returns>

      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             CharSet
    =System.Runtime.InteropServices.CharSet.Ansi,
             EntryPoint
    ="mysql_real_connect", ExactSpelling=true)]
        
    public static extern IntPtr Connect(IntPtr db,
          [In] 
    string host, [In] string user, [In] string passwd,
          [In] 
    string dbname,
          
    uint port, [In] string socketName, uint flags
          );

      
    ///<summary>Selects a database</summary>
      
    ///<returns>Zero for success.  Non-zero if an error occurred.</returns>

      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             CharSet
    =System.Runtime.InteropServices.CharSet.Ansi,
             EntryPoint
    ="mysql_select_db", ExactSpelling=true)]
        
    public static extern int SelectDb(IntPtr conn, [In] string db);

      
    ///<summary>Closes a server connection</summary>
      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll", EntryPoint="mysql_close", ExactSpelling=true)]
        
    public static extern void Close(IntPtr db);

      
    ///<summary>Executes a SQL query specified as a string</summary>
      
    ///<returns>number of rows changed, -1 if zero</returns>

      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             CharSet
    =System.Runtime.InteropServices.CharSet.Ansi,
             EntryPoint
    ="mysql_query", ExactSpelling=true)]
        
    public static extern int Query(IntPtr conn, [In] string query);

      
    ///<summary>Retrieves a complete result set to the client</summary>
      
    ///<returns>An IntPtr result structure with the results. IntPtr.Zero if an error occurred.</returns>

      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             EntryPoint
    ="mysql_store_result", ExactSpelling=true)]
        
    public static extern IntPtr StoreResult(IntPtr conn);

      
    ///<returns>Returns the number of rows in a result set</returns>
      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             EntryPoint
    ="mysql_num_rows", ExactSpelling=true)]
        
    public static extern int NumRows(IntPtr r);

      
    ///<returns>Returns the number of columns in a result set</returns>
      [SuppressUnmanagedCodeSecurity]
      [DllImport(
    "libmySQL.dll",
             EntryPoint
    ="mysql_num_fields", ExactSpelling=true)]
        
    public static extern int NumFields(IntPtr r);




           

  • 相关阅读:
    SQL SERVER 2008 SA禁用,Windows帐户被删
    SQL Server表结构修改脚本
    请教高手,如何取得Target属性
    Installing Reporting Services on Windows 7, Vista or Windows Server 2008 无权限(rsAccessDenied)解决方法
    请教:如何在子页面关闭时把焦点设置到父页面的服务器控件上?
    order by newid() sql随机查询
    JS干货,笔记大全
    破解XP密码
    直接连接*.mdf 文件 获取随机数据
    总结C#获取当前路径的7种方法(转)
  • 原文地址:https://www.cnblogs.com/Jimmy/p/43402.html
Copyright © 2011-2022 走看看