在MySQL的官方网站上提供有.net的连接器安装程序(mysql-connector-net-6.2.3.zip),下载后直接安装,在进行数据源连接的时候就会出现MySQL数据连接的选项。具体的连接方法和MSSQL的一样这里就不多说了。只是MySQL默认的管理员用户是root而不是sa,仅此而已。
连接器本身只提供以ADO.Net方式连接MySQL数据库,因此这里也仅以ADO.Net的方式介绍一下与MSSQL的不同:
1、在引用命名空间时需要使用using MySql.Data.MySqlClient,而不是using System.Data.SqlClinet;
2、在连接数据库时使用MySqlConnection,而不是SqlConnection;
3、在数据适配器时使用MySqlDataAdapter,而不是SqlDataAdapter;
4、执行SQL操作命令使用MySqlCommand,而不是SqlCommand;
以上是使用进行数据库开发常用的类,当然,其他的还有很多,也就是说在原来SqlClient的基础上前面都加上了一个MySql,方法和属性和SqlClient大致相同,就可以使很方便的将之前基于MsSQL开发的数据库应用程序转移到MySql数据库平台上。
以下是示例代码,演示的是在MySql的Test数据库的BaseUser表中添加和删除记录,红色字体代表与SqlClient不同之处,蓝色部分表示与SqlClient相同的方法,从代码中就可以看出,只需对原有代码进行小量修改就可以将现有的应用程序转移到MySql数据库上了:
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 MySql.Data.MySqlClient;//引用MySql的命名空间
namespace MySQLTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadData();
}
//从数据库中重新加载数据
private void LoadData()
{
using (MySqlConnectionthisConnection = newMySqlConnection(strConnectionString))
{
DataTable dt = new DataTable();
string strQueryText = "select * from BaseUser";
MySqlDataAdapterthisAdapter = newMySqlDataAdapter(strQueryText, thisConnection);
thisAdapter.Fill(dt);
dataGridView1.DataSource = dt;
}
}
//设定MySql数据库的连接字符
const string strConnectionString =" server=127.0.0.1;User Id=root;password=123;Persist Security Info=True;database=test";
//添加用户
private void btnAdd_Click(object sender, EventArgs e)
{
if (_txtUserID.Text != "" && _txtUserName.Text != "")
{
string strSqlCommand = "insert into BaseUser values('" +
_txtUserID.Text.Trim() + "','" + _txtUserName.Text.Trim() + "')";
ExecuteQuery(strSqlCommand);
LoadData();
}
}
//执行指定的SQL操作查询
private void ExecuteQuery(string strSqlCommand)
{
using (MySqlConnectionthisConnection = newMySqlConnection(strConnectionString))
{
thisConnection.Open();
MySqlCommandthisCommand = thisConnection.CreateCommand();
thisCommand.CommandText= strSqlCommand;
thisCommand.ExecuteNonQuery();
}
}
//删除用户
private void btnDelete_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
{
string strUserID = dr.Cells[0].Value.ToString().Trim();
ExecuteQuery("delete from BaseUser where ID ='" + strUserID.Trim() + "'");
}
LoadData();
}
}
}
}
转自:http://changmingzhi.blog.163.com/blog/static/29678048201068556452/
DataTable:数据表,和数据库的表一样
DataSet:数据集,里面由多个DataTable组成
MySQLDataAdapter:数据适配器,用于从数据库读取数据
//数据库连接对象(连接到指定数据)
SqlConnection conn = new SqlConnection("connection_string");
SqlCommand cmd = conn.CreateCommand();//命令对象(用来封装需要在数据库执行的语句)
cmd.CommandText = "select * from table";//设置sql文本
//第一种方式,用数据适配器
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);//这里也可以把数据查询结果填充到DataSet中
//第二种方式
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
//循环读取返回的数据
while (sdr.Read())
{
string name = sdr.GetString(0);
int age = sdr.GetInt32(1);
}
}
sdr.Close();
conn.Close();
//读取数据时SqlConnection和SqlCommand都是必须的
//获取数据时你可以用SqlDataAdapter + DataTable或DataSet)的方式
//也可以用SqlDataReader来循环遍历读取