天天看點

wpf連接配接資料庫

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)

        {

        }

    }

}