c#连接数据库

1、开发平台:VS2012     数据库:mysql

2、准备:需要在项目中导入DLL文件,下载地址:https://files.cnblogs.com/files/ywf520/mysql.zip

3、需求:登陆注册

4、界面展示:

 

5、代码:

        //用户登陆验证
        private void logUsePassword()
        {
            bool cs = false;
            MySqlCommand cmd = null;
            MySqlConnection con = null;
            try
            {      //连接数据库
                con = new MySql.Data.MySqlClient.MySqlConnection(
                   "Database='test';Data Source='localhost';User Id='用户名';Password='你的密码';charset='utf8'");//连接数据库
                con.Open();
                if (con != null)
                {
                    cmd = new MySqlCommand("select *from user", con);//查询语句
                    MySqlDataReader mysqldataread = cmd.ExecuteReader();
                    while (mysqldataread.Read())
                    {
                        if (mysqldataread.GetString(mysqldataread.GetOrdinal("userpassword")) == textBox2.Text &&
                            mysqldataread.GetString(mysqldataread.GetOrdinal("username")) == textBox1.Text)
                        {
                            this.Hide();
                            cs = true;
                            Thread speak = new Thread(voice);
                            speak.Start();
                            //MessageBox.Show("登陆成功 \n  ");
                            new Welcom(textBox1.Text).Show();
                            break;
                        }
                    }
                    if (cs == false)
                    {
                        MessageBox.Show("用户名或密码错误请重新输入 \n  ");
                    }
                    if (mysqldataread.ReadAsync() == null) { MessageBox.Show("用户名或密码错误请重新输入 \n  "); }
                }
                else { MessageBox.Show("数据连接失败"); }
            }
            catch (MySqlException e)
            {
                //MessageBox.Show("数据未连接,请联系管理员\n" + e.ToString());
            }
            finally
            {
                con.Close();
                cmd.Dispose();
            }
        }

注册:

        private void button1_Click(object sender, EventArgs e)
        {
            String username = textBox1.Text;
            String password = textBox2.Text;
            String useremile = textBox3.Text;
            String usertel= textBox4.Text;
            String userid = textBox5.Text;
            try
            {
                if (username == "" || password == "" || useremile == "" || usertel == "")
               {
                   MessageBox.Show("用户名、密码、email、电话号码不能为空");
               }
                else if (getusername() == true && textBox2.Text == textBox3.Text)//判断用户名是否存在
                {
                    if (mysqll() != null)
                    {
                        //String insert = "INSERT INTO `test`.`user` (`username`, `userpassword`, `email`, `tel`, `ID`)"+
                        //     "VALUES ('textBox1.Text','textBox2.Text','textBox3.Text','textBox4.Text','textBox5.Text')";
                        cmd = mysqll().CreateCommand(); //创建一个数据库命令行 
                        cmd.CommandText = "insert into user(username,userpassword,email,tel,ID) values(@un,@pw,@em,@tel,@uid)"; //数据库命令的内容(@un,@pw是点位符,将会在下面的语句中赋值) 
                        cmd.Parameters.AddWithValue("un", username); // 将username 赋值给命令行的 "un” 
                        cmd.Parameters.AddWithValue("pw", password); // 将Password 赋值给命令行的 "pw” 
                        cmd.Parameters.AddWithValue("em", useremile);
                        cmd.Parameters.AddWithValue("tel", usertel);
                        cmd.Parameters.AddWithValue("uid", userid);
                        //cmd.ExecuteNonQuery();                     // 执行数据库命令 
                        if (cmd.ExecuteNonQuery() == 1)
                        {
                            Thread zhuceSuccess = new Thread(voiceSpeak);
                            zhuceSuccess.Start();
                            //MessageBox.Show("注册成功 \n  ");
                            //Form.ScrollStateVScrollVisible = true;
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        else
                        {
                            Thread zhucebad = new Thread(voicebad);
                            zhucebad.Start();
                            MessageBox.Show("注册失败");
                        }
                    }
                }
            }
            finally
            {
                if (mysqll()!= null)
                {
                    mysqll().Close();
                }
            }
        }

        //检查用户名是否存在
        private Boolean getusername()
        {
            //连接数据库
            try
            {
                if (mysqll() != null)
                {
                    cmd = new MySqlCommand("select *from user", mysqll());//查询语句
                    MySqlDataReader mysqldataread = cmd.ExecuteReader();
                    while (mysqldataread.Read())
                    {
                        if (mysqldataread.HasRows)
                        {
                            if (mysqldataread.GetString(mysqldataread.GetOrdinal("username")) == textBox1.Text)
                            {
                                Thread zhucebad = new Thread(voicebad1);
                                zhucebad.Start();
                                label6.Text = "该用户名已存在!";
                                label6.ForeColor = Color.Red;
                                return false;
                            }
                        }
                    }
                }
            }
            catch (MySqlException E) { MessageBox.Show(E.ToString()); }
            finally
            {
                if (mysqll() != null)
                    mysqll().Close();
            }
            return true;
        }

        private MySqlConnection mysqll()
        {
            //连接数据库
            con = new MySql.Data.MySqlClient.MySqlConnection(
                "Database='test';Data Source='localhost';User Id='root';Password='你的数据库密码';charset='utf8'");//连接数据库
            con.Open();
            return con;
        }

 

posted @ 2017-03-11 16:48  天意天雨  阅读(294)  评论(0编辑  收藏  举报