private void bindView() { id = NavigationContext.QueryString["id"]; try { //连接数据库并初始化DataContext实例 noteDB = new NoteDataContext(NoteDataContext.DBConnectionString); // 使用Linq查询语句查询NoteTable表的所有数据 var notesInDB = from NoteTable note in noteDB.Notes where note.ID == Int32.Parse(id) select note; // 将查询的结果返回到页面数据绑定的集合里面 noteCol.NoteTables = new ObservableCollection <NoteTable>(notesInDB); TxtTitle.Text = noteCol.NoteTables.First().Title; TxtContent.Text = noteCol.NoteTables.First().Content; //将要删除的数据放到State中 State["note"] = noteCol.NoteTables.First(); } catch { MessageBox.Show("记事已经删除了!"); } }
// Code to execute when the application is launching (eg, from Start) // This code will not execute when the application is reactivated private void Application_Launching(object sender, LaunchingEventArgs e) { //如果数据库不存在则创建一个数据库 using (NoteDataContext db = new NoteDataContext(NoteDataContext.DBConnectionString)) { if (db.DatabaseExists() == false) { //创建一个数据库 db.CreateDatabase(); } } }
private void AppBar_Save_Click(object sender, EventArgs e) { if (this.TxtTitle.Text == "") { MessageBox.Show("请填写标题"); return; } if (this.TxtTitle.Text.Length > 10) { MessageBox.Show("标题不能超过10个字符"); return; } try { //连接数据库并初始化DataContext实例 noteDB = new NoteDataContext(NoteDataContext.DBConnectionString); //创建一条表的数据 NoteTable newNote = new NoteTable { Title = TxtTitle.Text, Time = DateTime.Now.ToLongDateString(), Content = TxtContent.Text.ToString() }; //添加绑定集合的数据 noteCol.NoteTables.Add(newNote); //插入数据库 noteDB.Notes.InsertOnSubmit(newNote); //保存数据库的改变 noteDB.SubmitChanges(); } catch (Exception) { MessageBox.Show("保存数据出错!"); } //保存完后直接跳到查询页面 NavigationService.Navigate(new Uri(string.Format("/MainPage.xaml"), UriKind.Relative)); }
//private void noteLocation_Click(object sender, RoutedEventArgs e) //{ // HyperlinkButton clickedLink = (HyperlinkButton)sender; // string uri = String.Format("/Note;component/ViewEdit.xaml?id={0}", clickedLink.Tag); // NavigationService.Navigate(new Uri(uri, UriKind.Relative)); //} private void bindList() { try { //连接数据库并初始化DataContext实例 noteDB = new NoteDataContext(NoteDataContext.DBConnectionString); // 使用Linq查询语句查询EmployeeTable表的所有数据 var notesInDB = from NoteTable note in noteDB.Notes select note; // 将查询的结果返回到页面数据绑定的集合里面 noteCol.NoteTables = new ObservableCollection <NoteTable>(notesInDB); //赋值给当前页面的DataContext用于数据绑定 this.DataContext = noteCol; noteListBox.ItemsSource = noteCol.NoteTables; } catch { MessageBox.Show("数据库查询错误!"); } }