<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;
}
}
}
}
}
}