1. 程式人生 > >WPF DataGrid顯示MySQL查詢資訊,且可刪除、修改、插入

WPF DataGrid顯示MySQL查詢資訊,且可刪除、修改、插入

c# MySQL

1、入行好幾年了,工作中使用資料庫機率很小(傳統行業)。藉著十一假期回家機會,學習下資料庫。

2、初次瞭解資料庫相關知識,如果本文有誤,還望告知。

3、本文主要目的,記錄下wpf介面顯示資料庫資訊,且可進行刪除、修改、插入命令。並反饋資料到MySQL。做個記錄,以便以後工作中使用到時沒個頭緒。

4、MySQL的基本講解不再介紹,安裝過程什麼的,我也是按照網上教程一步一步進行的,假定MySQL已安裝成功,且新建有資料庫,見下圖:

在這裡插入圖片描述

廢話不多說,直接上程式碼

介面程式碼xaml

    <Grid>
        <DataGrid x:Name="DataGrid1" HorizontalAlignment="Left" Height="400" Margin="10,10,0,0" VerticalAlignment="Top" Width="537" LoadingRow="DataGrid_LoadingRow">
            <DataGrid.Columns>
                <DataGridTextColumn Header="id" Width="50" Binding="{Binding Path=id}"/>
                <DataGridTextColumn Header="name" Width="*" Binding="{Binding Path=name}"/>
                <DataGridTextColumn Header="phone" Width="*" Binding="{Binding Path=phone}"/>
                <DataGridTextColumn Header="email" Width="*" Binding="{Binding Path=email}"/>
            </DataGrid.Columns>
        </DataGrid>
        <Button x:Name="DeleteButton" Content="刪除" Margin="0,40,10,0" VerticalAlignment="Top" Click="DeleteButton_Click" HorizontalAlignment="Right" Width="75"/>
        <Button x:Name="UpdateButton" Content="修改" Margin="0,80,10,0" VerticalAlignment="Top" Click="UpdateButton_Click" HorizontalAlignment="Right" Width="75"/>
        <Button x:Name="InsertButton" Content="插入" Margin="0,120,10,0" VerticalAlignment="Top" Click="InsertButton_Click" HorizontalAlignment="Right" Width="75"/>
    </Grid>

後端程式碼cs

    public partial class MainWindow : Window
    {
        //SQLBulkCopy
        Random rd = new Random();
        string sqlstr = "Data Source=127.0.0.1;User ID=root;Password=root;DataBase=test;Charset=utf8;";
        MySql.Data.MySqlClient.MySqlConnection con;
        MySql.Data.MySqlClient.MySqlDataAdapter adapter;
        System.Data.DataSet ds;
        System.Data.DataTable dt;


        public MainWindow()
        {
            InitializeComponent();

            UpdateMySQLData();
        }

        private void DataGrid_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        private void UpdateMySQLData()
        {
            if (con == null)
            {
                con = new MySql.Data.MySqlClient.MySqlConnection(sqlstr);
                con.Open();
            }
            if (adapter == null)
            {
                adapter = new MySql.Data.MySqlClient.MySqlDataAdapter("select * from user", con);
            }
            if (ds == null)
            {
                ds = new System.Data.DataSet();
            }
            ds.Clear();
            adapter.Fill(ds, "user");
            if (dt == null)
            {
                dt = ds.Tables["user"];
            }
            DataGrid1.ItemsSource = dt.DefaultView;
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            int index = DataGrid1.SelectedIndex;
            if (index == -1) return;
#if MySQLCommand
            string DeleteSqlCommand = string.Format("delete from user where id = '{0}'", dt.Rows[index]["id"]);
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(DeleteSqlCommand, con);
            cmd.ExecuteNonQuery();

            UpdateMySQLData();
#else
            dt.Rows[index].Delete();
            //dt.Rows.RemoveAt(index);==dt.Rows[index].Delete() + dt.AcceptChanges()
            MySql.Data.MySqlClient.MySqlCommandBuilder builder = new MySql.Data.MySqlClient.MySqlCommandBuilder(adapter);
            adapter.Update(dt);
            dt.AcceptChanges();
#endif
        }

        private void UpdateButton_Click(object sender, RoutedEventArgs e)
        {
#if MySQLCommand
            int index = DataGrid1.SelectedIndex;
            string UpdateSqlCommand = string.Format("update user set id = '{0}', name = '{1}', phone = '{2}', email = '{3}' where id = '{0}'",
                dt.Rows[index]["id"], dt.Rows[index]["name"], dt.Rows[index]["phone"], dt.Rows[index]["email"]);
            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(UpdateSqlCommand, con);
            cmd.ExecuteNonQuery();

            UpdateMySQLData();
#else
            MySql.Data.MySqlClient.MySqlCommandBuilder builder = new MySql.Data.MySqlClient.MySqlCommandBuilder(adapter);
            adapter.Update(dt);
            dt.AcceptChanges();
#endif
        }

        private void InsertButton_Click(object sender, RoutedEventArgs e)
        {
#if MySQLCommand
            string InsertSqlCommand = string.Format("insert into user(id, name, phone,email) values('{0}','{1}','{2}','{3}')", rd.Next(100), "ZhangSan", 12332112345, "
[email protected]
"); MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(InsertSqlCommand, con); cmd.ExecuteNonQuery(); string InsertSqlCommand2 = string.Format("insert into user(id, name, phone,email) values('{0}','{1}','{2}','{3}')", rd.Next(100), "LiSi", 12332112345, "[email protected]"); MySql.Data.MySqlClient.MySqlCommand cmd2 = new MySql.Data.MySqlClient.MySqlCommand(InsertSqlCommand2, con); cmd2.ExecuteNonQuery(); UpdateMySQLData(); #else System.Data.DataRow dr = dt.NewRow(); dr[0] = rd.Next(100); dr[1] = "ZhangSan"; dr[2] = "12332112345"; dr[3] = "[email protected]"; dt.Rows.Add(dr); System.Data.DataRow dr2 = dt.NewRow(); dr2[0] = rd.Next(100); dr2[1] = "LiSi"; dr2[2] = "12332154321"; dr2[3] = "[email protected]"; dt.Rows.Add(dr2); MySql.Data.MySqlClient.MySqlCommandBuilder builder = new MySql.Data.MySqlClient.MySqlCommandBuilder(adapter); adapter.Update(ds, "user"); dt.AcceptChanges(); #endif } }

軟體開啟介面
在這裡插入圖片描述

刪除時一直不失敗,網上找了好久才找到答案
參考資料
https://blog.csdn.net/sz101/article/details/5837950
https://bbs.csdn.net/wap/topics/390845652
http://www.cnblogs.com/perfect/archive/2007/08/06/844634.html