您的位置:首页 > 数据库 > MySQL

c#连接mysql 数据库 绝对能用

2013-06-04 10:36 337 查看
以下可以查询出在example数据库中person表中的所有信息

并且显示在datagrid中MySQLDriverCS 自己在网上随便找找就有一堆

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;

namespace sqltest
{
    public partial class Form1 : Form
    {
        MySQLDataAdapter mda;
        DataSet ds;
        public Form1()
        {
            InitializeComponent();
            aa();
        }

        public void aa()
        {
            MySQLConnection conn = null;
            try
            {
                string connstr = "Data Source=webexample;" + "Password=;User ID=root;Location=localhost";
                conn = new MySQLConnection(connstr);
                conn.Open();
                MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
                commn.ExecuteNonQuery();

                string sql = "select * from person ";
                mda = new MySQLDataAdapter(sql, conn);
                ds = new DataSet("MyData");
                mda.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception ex){
                MessageBox.Show(ex.Message);
            }

            finally{
                conn.Close();
            }
        }

    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: