日期:2014-05-18 浏览次数:21253 次
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.SqlClient; using System.Data; namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { SqlDataAdapter sda; DataTable dt; void getData() { //init sqlconnection SqlConnectionStringBuilder connbuilder = new SqlConnectionStringBuilder(); connbuilder.DataSource = "(local)"; connbuilder.IntegratedSecurity = true; connbuilder.InitialCatalog = "season"; //start to make sql query SqlConnection conn = new SqlConnection(connbuilder.ConnectionString); sda = new SqlDataAdapter("select Number,name,grade,sex from season.students;", conn); SqlCommandBuilder commbuilder = new SqlCommandBuilder(sda); sda.UpdateCommand = commbuilder.GetUpdateCommand(); dt = new DataTable(); sda.AcceptChangesDuringUpdate = true; sda.Fill(dt); listView1.ItemsSource = dt.DefaultView; } private void button1_Click(object sender, RoutedEventArgs e) { getData(); } public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { } } }
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="328" Width="551" Loaded="Window_Loaded"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> <RowDefinition Height="22" /> </Grid.RowDefinitions> <ListView Name="listView1" MinWidth="280" > <ListView.View> <GridView x:Name="gridView1"> <GridViewColumn Header="ContactID" DisplayMemberBinding="{Binding Path=Number}"></GridViewColumn> <GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding Path=name}"></GridViewColumn> <GridViewColumn Header="LastName" DisplayMemberBinding="{Binding Path=grade}"></GridViewColumn> <GridViewColumn Header="EmailAddress" DisplayMemberBinding="{Binding Path=sex}"></GridViewColumn> </GridView> </ListView.View> </ListView> <WrapPanel Grid.Row="1" Orientation="Horizontal"></WrapPanel> <Button Grid.Row="2" HorizontalAlignment="Right" Click="button1_Click" Name="butt