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;

namespace Ch12Sample_03
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private string connString;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            PubsDataContext data
                 = new PubsDataContext(connString);
            var myquery =
                from title in data.titles
                select title;
            textBox1.Text = "";
            foreach (var title in myquery)
            {
                textBox1.Text += title.title_id + "/" + title.title + "/" + title.price + Environment.NewLine;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            connString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\pubs.mdf;Integrated Security=True;User Instance=True";
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            PubsDataContext data
                 = new PubsDataContext(connString);
            var myquery =
                from title in data.titles
                where title.price > 20
                select title;
            textBox1.Text = "";
            foreach (var title in myquery)
            {
                textBox1.Text += title.title_id + "/" + title.title + "/" + title.price + Environment.NewLine;
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            PubsDataContext pubs
                 = new PubsDataContext(connString);

            titles title = new titles();
            title.title_id = "AA1234";
            title.title = "C# 2008最佳实务讲座";
            title.type = "测试用";
            title.price = 800;
            title.pubdate = DateTime.Now;

            pubs.titles.InsertOnSubmit(title);
            try
            {
                pubs.SubmitChanges();
                MessageBox.Show("数据添加成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("发送异常，请检查是否加入重复数据");
            }
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            PubsDataContext pubs
                = new PubsDataContext(connString);

            var titleQuery =
                from title in pubs.titles
                where title.title_id == "AA1234"
                select title;
            foreach (var title in titleQuery)
            {
                title.price = 25;
            }

            pubs.SubmitChanges();
            MessageBox.Show("数据变更成功!");
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            PubsDataContext pubs
                 = new PubsDataContext(connString);

            var titleQuery =
                from title in pubs.titles
                where title.title_id == "AA1234"
                select title;
            if (titleQuery.Count() > 0)
            {
                pubs.titles.DeleteOnSubmit(titleQuery.First());
                pubs.SubmitChanges();
            }
        }


    }
}
