我的百度空间 我的51CTO空间 我的163空间

c#连接MYSQL数据

之前用PHP写了一个库存的网站,写得太差的了不能算网站,用的数据库是MYSQL5.几的。用的全是utf8编码

在C#里连接MYSQL ,开始用的是MySQLDriverCS 但是连接进去,显示中文时乱码。

在网上查了下用MySQL Connector Net 这个就正常了。[官网有得下]

用法与sqlclient里的自带函数一样。

注意:要在添加引用里引用 Mysql.Data

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using MySQLDriverCS;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace importTxtToMysql
{
public partial class Form2 : Form
{
private DataSet dsall;
//private static String mysqlcon = "Data Source=MySQL;database=onepc;Password=;User ID=root;Location=192.168.1.168;charset=utf8";
private static String mysqlcon = "database=onepc;Password=;User ID=root;server=192.168.1.168";//Data Source=MySQL;;charset=utf8";
private MySqlConnection conn;
private MySqlDataAdapter mdap;
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

try
{
// MySqlCommand cmd = new MySqlCommand("select ");
mdap = new MySqlDataAdapter("select * from allhard",conn);
dsall = new DataSet();
mdap.Fill(dsall,"hard");
dataGridView1.DataSource = dsall.Tables["hard"];


}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{

}

}

private void Form2_Load(object sender, EventArgs e)
{
conn = new MySqlConnection(mysqlcon);
}
}
}

 

posted on 2011-11-09 17:23  心若冰清  阅读(25134)  评论(0编辑  收藏  举报

导航