WPF与MySql数据库连接

来源:互联网 发布:hspa 什么网络 编辑:程序博客网 时间:2024/06/17 03:54

出处:http://blog.csdn.net/weiwei1994/article/details/50478477?ref=myread

using System;

using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySQLDriverCS;


namespace 监考1._0
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        public static MySqlCommand getSqlCommand(String sql, MySqlConnection mysql)
        {
            MySqlCommand mySqlCommand = new MySqlCommand(sql, mysql);
            //  MySqlCommand mySqlCommand = new MySqlCommand(sql);
            // mySqlCommand.Connection = mysql;
            return mySqlCommand;
        }

        public static void getDel(MySqlCommand mySqlCommand)
        {
            try
            {
                mySqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                String message = ex.Message;
                Console.WriteLine("删除数据失败了!" + message);
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            String mysqlStr = "server=localhost;User Id=root;password=admin;Database=stu_DB";
            MySqlConnection mysql = new MySqlConnection(mysqlStr);
            String sqlSearch = "select * from users";
            String sqlDel = "delete from users where username = '0000'";
            Console.WriteLine(sqlDel);
            MySqlCommand mySqlCommand = getSqlCommand(sqlDel, mysql);
            mysql.Open();
            getDel(mySqlCommand);
            mysql.Close();
            String readLine = Console.ReadLine();
            
            if (textBox1.Text.Trim() == "admin")
            {
                if (passwordBox1.Password.Trim() == "123")
                {
                    MessageBox.Show("登录成功!");
                    //this.Hide();
                    Window1 NewWindow1 = new Window1();
                    NewWindow1.Show();
                }
                else
                {
                    MessageBox.Show("密码错误!");
                }
            }
            else
            {
                MessageBox.Show("用户名错误!");
            }



     //       var temp = this.passwordBox1.Password;//获取密码框的内容
      //      MessageBox.Show(temp);//提示框输出

        }

        private void passwordBox1_PasswordChanged(object sender, RoutedEventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}
0 0