这是一个简单的用VS2005实现的连接MySQL的小程序
界面如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace mysqlv
{
public partial class Form1 : Form
{
MySqlConnection myconn;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
myconn = new MySqlConnection("Data Source=localhost;Initial Catalog=abc;" + "User ID=root;PWD=***");
myconn.Open();
}
catch (MySqlException myerror) {
MessageBox.Show("MySQL connection error:"+ myerror.Message);
this.Close();
}
button2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
MySqlCommand com;
MySqlDataReader dr;
com = new MySqlCommand("select name,sex,birth,birthaddr from user",myconn);
dr = com.ExecuteReader();
while (dr.Read())
textBox1.AppendText("name=" + dr["name"] + ",sex=" + dr["sex"] + ",birth=" + dr["birth"] + ",birthaddr=" + dr["birthaddr"] + Environment.NewLine);
dr.Close();
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
}
}