|
Posted on
2004-09-15 15:58
抱抱龙
阅读( 2459)
评论()
编辑
收藏
举报
写完“在.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);
另外在网友 的提示下去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);
另外在网友 的提示下去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);
|
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· 《HelloGitHub》第 108 期
· MQ 如何保证数据一致性?
· 一个基于 .NET 开源免费的异地组网和内网穿透工具