飞来飞去简易教程-实现数据库链接+用户登录+注册+删除

下面的桌面应用程序,实现数据库链接+用户登录+注册+删除

1.打开应用程序

2.点击“链接数据库”,数据库链接成功

3.点击“获得用户”,获得目前数据库中的用户信息

4.注册用户:输入用户名和密码(此处密码未隐藏)

5.点击“注册”,注册成功

6.再点击“获得用户”,刚注册的用户已添加到数据库中

7.登录用户:输入用户名和密码

8.点击“登录按钮”,登录成功

9.删除用户:选择要删除的用户名

10.点击“删除用户”,删除成功

12.再点击“获得用户”,可发现刚删除的用户名不见了。

 

程序源码如下:

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;

namespace MysqlTest1
{
    public partial class Form1 : Form
    {
        MySqlConnection conn = null;//在类里面定义变量
        public Form1()
        {
            InitializeComponent();
                                    //在构造函数里面初始化
            conn = new MySqlConnection("Data Source=localhost;Initial Catalog=test;User ID=root;Password=mysql;");
                                    //在方法里面使用
        }

        private void btnLink_Click(object sender, EventArgs e)
        {
            conn.Open();        //打开对象
            if (conn.State.ToString() == "Open")
            {
                lab_state.Text  = "数据库连接成功!";
            }
            else
            {
                lab_state.Text = "数据库连接失败!";
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            conn.Close();
            if(conn.State.ToString()=="Closed")
            {
                lab_state.Text = "数据库已关闭!";
            }
        }

        private void btn_load_Click(object sender, EventArgs e)
        {
            string userName=txt_User.Text.Trim();       //获取用户名
            string password = txt_Password.Text.Trim(); //获取密码
            string CmdStr = "Select * from test.user where name='"+userName+"' AND password='"+password+"'";
            MySqlCommand cmd = new MySqlCommand(CmdStr,conn);   //定义查询语句
           
            MySqlDataReader sreader = cmd.ExecuteReader();      //执行查询操作
            if (sreader.Read())
            {
                MessageBox.Show("登录成功!");
            }
            else
            {
                MessageBox.Show("登录失败!");
            }
            sreader.Close();        //每次执行完毕都得关闭reader
        }

        private void btn_Sign_Click(object sender, EventArgs e)
        {
            string userName = txt_User.Text.Trim();         //获取文本信息,注意去掉多余空格
            string password = txt_Password.Text.Trim();

            string CmdStr = "INSERT INTO user VALUES('"+userName+"','"+password+"')";
            MySqlCommand cmd = new MySqlCommand(CmdStr,conn);
            int i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                MessageBox.Show("注册成功!");
            }

        }

        private void btn_GetUsers_Click(object sender, EventArgs e)
        {
            comb_users.Items.Clear();               //清除下拉列表框
            MySqlCommand cmd = null;                //新建MySqlCommad对象
            MySqlDataReader dr = null;              //新建MySqlDataReader对象
            string CmdStr = "SELECT name FROM user";//Mysql删除命令
            cmd = new MySqlCommand(CmdStr,conn);    //将MySqlCommand对象实例化
            dr = cmd.ExecuteReader();               //调用ExecuteReader方法,执行查找命令
            while(dr.Read())                        //调用Read方法读取查询结果的记录,“有”为true,“无”为false
            {
                comb_users.Items.Add(dr["name"]);   //向列表中添加用户名
            }
            dr.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string username = comb_users.Text.Trim();               //获取用户名
            string CmdStr = "DELETE FROM user WHERE name='"+username+"'";//Mysql删除命令
            MySqlCommand cmd = new MySqlCommand(CmdStr,conn);       //创建MySqlCommand对象
            int i = cmd.ExecuteNonQuery();                          //执行删除命令
            if (i > 0)
            {
                MessageBox.Show("用户删除成功!");
            }
            else
            {
                MessageBox.Show("用户删除失败!");
            }

        }  
    }
}

 

 

posted @ 2013-04-12 21:50  在路上的人  阅读(211)  评论(0编辑  收藏  举报