WPF数据操作

<Window x:Class="DataBaseOper.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="9*" />
            <ColumnDefinition Width="494*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="245*" />
            <RowDefinition Height="66*" />
        </Grid.RowDefinitions>
        <DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" Margin="3,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479" SelectionChanged="dataGrid1_SelectionChanged" Grid.Column="1" />
        <Button Content="显示" Height="23" HorizontalAlignment="Left" Margin="38,31,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click_1" Grid.Row="1" Grid.Column="1" />
        <Button Content="删除" Height="23" HorizontalAlignment="Left" Margin="130,31,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" Grid.Row="1" Grid.Column="1" />
        <Button Content="添加" Height="23" HorizontalAlignment="Left" Margin="230,31,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="button3_Click" Grid.Row="1" Grid.Column="1" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="59,218,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Grid.Column="1" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="185,218,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Grid.Column="1" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="311,218,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" Grid.Column="1" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="59,2,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" Grid.Row="1" Grid.Column="1" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="311,2,0,0" Name="textBox5" VerticalAlignment="Top" Width="120" Grid.Row="1" Grid.Column="1" />
        <Button Content="修改" Height="23" HorizontalAlignment="Right" Margin="0,31,91,0" Name="button4" VerticalAlignment="Top" Width="75" Click="button4_Click" Grid.Row="1" Grid.Column="1" />
    </Grid>
</Window>

 

using System;
using System.Collections.Generic;
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 System.Data;
using System.Data.SqlClient;
using System.Configuration;

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

     

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            Disp("select * from T_Excel",CommandType.Text);
        }

        private void Disp(string sqlcmd,CommandType commandtype,params SqlParameter[] param)
        {
            using (SqlConnection con = new SqlConnection(@"server=.\sqlexpress;initial catalog=Mytest1;integrated security=true"))
            {
                using (SqlCommand cmd = new SqlCommand(sqlcmd, con))
                {
                    cmd.Parameters.AddRange(param);
                    cmd.CommandType = commandtype;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        dataGrid1.ItemsSource = dt.DefaultView;
                    }
                }
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
          // MessageBox.Show(dataGrid1.SelectedIndex.ToString());
           DataRowView row = dataGrid1.SelectedItem as DataRowView;
           if (row!=null)
           {
               SQLExecute("delete from T_Excel where id=@id",CommandType.Text,new SqlParameter("@id",row["id"].ToString()));
               Disp("select * from T_Excel",CommandType.Text);
               MessageBox.Show("删除成功!!!");
           }

           
        }


        private void SQLExecute(string sqlcmd,CommandType commandtype, params SqlParameter[] param)
        {
            using (SqlConnection con = new SqlConnection(@"server=.\sqlexpress;initial catalog=Mytest1;integrated security=true"))
            {
                using (SqlCommand cmd = new SqlCommand(sqlcmd, con))
                {
                    cmd.Parameters.AddRange(param);
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
            }
       
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            SQLExecute("insert T_Excel values(@class,@stuid,@name,@phone,@QQ)",
                CommandType.Text, new SqlParameter("@class", textBox1.Text),
                new SqlParameter("@stuid", textBox2.Text.Trim()),
                new SqlParameter("@name", textBox3.Text.Trim()),
                new SqlParameter("@phone", textBox4.Text.Trim()),
                new SqlParameter("@QQ", textBox5.Text.Trim())
                );

            Disp("select * from T_Excel",CommandType.Text);
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
             DataRowView row = dataGrid1.SelectedItem as DataRowView;
             if (row != null)
             {
                 SQLExecute("update T_Excel set class=@class,stuid=@stuid,name=@name,phone=@phone,qq=@QQ where id=@id",
                    CommandType.Text, new SqlParameter("@class", textBox1.Text),
                    new SqlParameter("@stuid", textBox2.Text.Trim()),
                    new SqlParameter("@name", textBox3.Text.Trim()),
                    new SqlParameter("@phone", textBox4.Text.Trim()),
                    new SqlParameter("@QQ", textBox5.Text.Trim()),
                    new SqlParameter("@id",row["id"].ToString())
                    );
             }
             Disp("select * from T_Excel",CommandType.Text);
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
             DataRowView row = dataGrid1.SelectedItem as DataRowView;
             if (row != null)
             {
                DataTable dt = ReturnDisp("select * from T_Excel where id=@id",CommandType.Text,new SqlParameter("@id",row["id"].ToString()));
                 textBox1.Text=dt.Rows[0][0].ToString();
                 textBox2.Text=dt.Rows[0][1].ToString();;
                 textBox3.Text=dt.Rows[0][2].ToString();;
                 textBox4.Text=dt.Rows[0][3].ToString();;
                 textBox5.Text=dt.Rows[0][4].ToString();;
               
             }
        }

        private DataTable ReturnDisp(string sqlcmd, CommandType commandtype, params SqlParameter[] param)
        {
            using (SqlConnection con = new SqlConnection(@"server=.\sqlexpress;initial catalog=Mytest1;integrated security=true"))
            {
                using (SqlCommand cmd = new SqlCommand(sqlcmd, con))
                {
                    cmd.Parameters.AddRange(param);
                    cmd.CommandType = commandtype;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值