none
WPF 浏览器程序应如何连接SQL RRS feed

答案

  • 您好,

     

    下面是一个连接SQL Server数据库的小例子,希望对你有用。

     

    XAML 代码:

    <Window x:Class="WpfSimpleDataGrid.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

         xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"

         Width="400" Height="300"

         WindowStartupLocation="CenterScreen"

        Title="Window1" >

        <Grid>

            <Grid.RowDefinitions>

                <RowDefinition />

                <RowDefinition />

                <RowDefinition />

                <RowDefinition />

                <RowDefinition />

                <RowDefinition />

                <RowDefinition />

            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>

                <ColumnDefinition />

                <ColumnDefinition />

                <ColumnDefinition />

            </Grid.ColumnDefinitions>

            <TextBlock Text="ID" Grid.Row="0" Grid.Column="0" />

            <TextBlock Text="Name" Grid.Row="1" Grid.Column="0" />

            <TextBox Name="insertIDTextBox"  Grid.Row="0" Grid.Column="1"/>

            <TextBox Name="insertNameTextBox" Grid.Row="1" Grid.Column="1"/>

            <Button Click="OnInsertData"  Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"

                    Width="80" Height="30" Content="Insert Data"  />

            <TextBlock Text="Input ID" Grid.Row="3" Grid.Column="0" />

            <TextBox Name="searchTextBox"  Grid.Row="3" Grid.Column="1"/>

            <Button Click="OnSearchData"  Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2"

                    Width="80" Height="30" Content="Search Data"  />

            <dg:DataGrid Name="dataGrid" Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3" Grid.RowSpan="3" />

        </Grid>

    </Window>

     

    后台代码: 

    using System;

    using System.Windows;

    using System.Data;

    using System.Data.SqlClient;

    namespace WpfSimpleDataGrid

    {

        public partial class Window1 : Window

        {

            public Window1()

            {

                InitializeComponent();

            }

            private void OnInsertData(object sender, RoutedEventArgs e)

            {

                SqlConnection conn = ConnectionHelper.GetConnection();

                SqlCommand cmd = conn.CreateCommand();

                cmd.CommandText = "insert into Category values(" + insertIDTextBox.Text.Trim() + ",'" + insertNameTextBox.Text.Trim() + "')";

                try

                {

                    conn.Open();

                    cmd.ExecuteNonQuery();

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

                finally

                {

                    if (conn.State == ConnectionState.Open)

                    {

                        conn.Close();

                    }

                }

            }

            private void OnSearchData(object sender, RoutedEventArgs e)

            {

                SqlConnection conn = ConnectionHelper.GetConnection();

                SqlCommand cmd = conn.CreateCommand();

                string strSearch = searchTextBox.Text.Trim();

                if (strSearch == "")

                {

                    cmd.CommandText = "select * from Category";

                }

                else

                {

                    cmd.CommandText = "select * from Category where CategoryID=" + searchTextBox.Text.Trim();

                }

                SqlDataReader reader;

                DataTable CategroyTable = new DataTable();

                try

                {

                    conn.Open();

                    reader = cmd.ExecuteReader();

                    CategroyTable.Load(reader);

                    dataGrid.ItemsSource = CategroyTable.DefaultView;

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

                finally

                {

                    if (conn.State == ConnectionState.Open)

                    {

                        conn.Close();

                    }

                }

            }

        }

        public class ConnectionHelper

        {

            public static SqlConnection GetConnection()

            {

                string connectionStr = "Data Source=MICROSOFT-JIGUO;Initial Catalog=CompanyTestDB;Integrated Security=True";

                SqlConnection conn = new SqlConnection(connectionStr);

                return conn;

            }

        }

    }

     

    如果你还有其他不明白的地方,请继续回贴。


    Jim Zhou -MSFT
    2009年5月5日 12:44
    版主