码迷,mamicode.com
首页 > 数据库 > 详细

C# 连接 MySQL

时间:2018-04-04 21:20:50      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:eric   catch   rms   pos   database   lan   public   sql   stat   

C#如何操作MySQL资料库?

MySQL是有出过针对.NET的连接器(Connector),其实就是一些.NET DLL,大家可以去https://dev.mysql.com/downloads/connector/net/这个地址去找针对对应的.NET版本的连接器。

 

技术分享图片

 

由于我当前电脑的.NET 版本是4.5.0的,所以我应该下载 6.9.11版本的连接器 :

技术分享图片

 

 

具体连接器对应.NET的版本可以点开https://dev.mysql.com/doc/connector-net/en/connector-net-versions.html进行查看:

 

技术分享图片

 

当你下载并安装完毕之后,去到连接器的安装根目录

在你的C#项目里添加这个DLL做引用,并Using MySQL的命名空间:

技术分享图片

 

 使用例子如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace MySQLTest
{
    public partial class Form1 : Form
    {
        Form2 formMain = new Form2();
        MySqlConnection m_Conn = new MySqlConnection();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            m_Conn.ConnectionString = "Server=localhost;Port=3306;Database=test; User=root;Password=root;";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "";
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    if (m_Conn.State != ConnectionState.Open)
                    {
                        m_Conn.Open();
                    }
                    sql = "select name,pass from userinfo where name=@NAME and pass=@PASSWD";

                    cmd.Parameters.AddWithValue("@NAME", textBox1.Text.Trim());
                    cmd.Parameters.AddWithValue("@PASSWD", textBox2.Text.Trim());

                    cmd.Connection = m_Conn;
                    cmd.CommandText = sql;

                    MySqlDataReader reader = cmd.ExecuteReader();

                    if (reader.Read())
                    {
                        if (reader["name"].ToString().CompareTo(textBox1.Text.Trim()) == 0 && reader["pass"].ToString().CompareTo(textBox2.Text.Trim()) == 0 )
                        {
                            this.Hide();
                            formMain.ShowDialog();
                            this.Show();
                        }
                    }
                    else
                    {
                        MessageBox.Show("帐号或密码错误,请检查!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    reader.Close();
                }

            }
            catch (Exception) 
            {
                
                throw;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (m_Conn.State != ConnectionState.Closed)
                {
                    m_Conn.Close();
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                button1_Click(sender, e);
            }
        }
    }
}

 

C# 连接 MySQL

标签:eric   catch   rms   pos   database   lan   public   sql   stat   

原文地址:https://www.cnblogs.com/Adin/p/8718716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!