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);




           

  • 相关阅读:
    成佛、远不止渡沧海
    导航栏中各按钮在点击当前按钮变色其他按钮恢复为原有色的实现方法(vue、jq、原生js)
    vue动态绑定src加字符串拼接
    对象中那些不注意的用法
    vue实现实时监听文本框内容的变化(最后一种为原生js)
    table
    toFixed()精度丢失;复选框全选、取消
    vue.js
    vue项目知识点总结
    JVM基础知识总结
  • 原文地址:https://www.cnblogs.com/Jimmy/p/43402.html
Copyright © 2011-2022 走看看