private void Save_Click(object sender, RoutedEventArgs e) { if (iD_C_MTextBox.Text != "") { try { if ((contraindicationTextBox.Text == "") || (contraindicationTextBox1.Text == "")) { MessageBox.Show("Данные не удалось обновить"); } else { DB_Maket.ASSDataSet aSSDataSet = ((DB_Maket.ASSDataSet)(this.FindResource("aSSDataSet"))); DB_Maket.ASSDataSetTableAdapters.Met_DTableAdapter aSSDataSetMet_DTableAdapter = new DB_Maket.ASSDataSetTableAdapters.Met_DTableAdapter(); aSSDataSetMet_DTableAdapter.Update(aSSDataSet.Met_D); } MessageBox.Show("Данные успешно обновлены"); } catch (System.Exception ex) { MessageBox.Show("Данные не удалось обновить"); } } else { if (contraindicationTextBox.Text == "") { MessageBox.Show("Не удалось добавить запись"); } else { try { SqlConnection con = new SqlConnection("Data Source=DESKTOP-EUL68A7\\SQLEXPRESS;Initial Catalog=ASS;Integrated Security=True"); con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = "INSERT INTO dbo.Met_D (Method,Disease) VALUES (" + contraindicationTextBox.Text + "," + contraindicationTextBox1.Text + ");"; cmd.ExecuteNonQuery(); con.Close(); contraindicationTextBox.Text = ""; contraindicationTextBox1.Text = ""; DB_Maket.ASSDataSet aSSDataSet = ((DB_Maket.ASSDataSet)(this.FindResource("aSSDataSet"))); DB_Maket.ASSDataSetTableAdapters.Met_DTableAdapter aSSDataSetMet_DTableAdapter = new DB_Maket.ASSDataSetTableAdapters.Met_DTableAdapter(); aSSDataSetMet_DTableAdapter.Fill(aSSDataSet.Met_D); System.Windows.Data.CollectionViewSource met_DViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("met_DViewSource"))); met_DViewSource.View.MoveCurrentToFirst(); MessageBox.Show("Запись успешно добавлена"); } catch { MessageBox.Show("Не удалось добавить запись"); } } } }
private void Window_Loaded(object sender, RoutedEventArgs e) { DB_Maket.ASSDataSet aSSDataSet = ((DB_Maket.ASSDataSet)(this.FindResource("aSSDataSet"))); // Load data into the table Met_D. You can modify this code as needed. DB_Maket.ASSDataSetTableAdapters.Met_DTableAdapter aSSDataSetMet_DTableAdapter = new DB_Maket.ASSDataSetTableAdapters.Met_DTableAdapter(); aSSDataSetMet_DTableAdapter.Fill(aSSDataSet.Met_D); System.Windows.Data.CollectionViewSource met_DViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("met_DViewSource"))); met_DViewSource.View.MoveCurrentToFirst(); }
private void Delete_Click(object sender, RoutedEventArgs e) { string mbtext = "Удалить запись?"; MessageBoxButton mbutton = MessageBoxButton.OKCancel; MessageBoxImage micon = MessageBoxImage.Question; MessageBoxResult Result = MessageBox.Show(mbtext, "", mbutton, micon); switch (Result) { case MessageBoxResult.OK: { try { SqlConnection con = new SqlConnection("Data Source=DESKTOP-EUL68A7\\SQLEXPRESS;Initial Catalog=ASS;Integrated Security=True"); con.Open(); string sqlstr = "DELETE FROM dbo.Met_D WHERE ID_Met_D = " + iD_C_MTextBox.Text; SqlDataAdapter SDA = new SqlDataAdapter(sqlstr, con); SDA.SelectCommand.ExecuteNonQuery(); con.Close(); DB_Maket.ASSDataSet aSSDataSet = ((DB_Maket.ASSDataSet)(this.FindResource("aSSDataSet"))); DB_Maket.ASSDataSetTableAdapters.Met_DTableAdapter aSSDataSetMet_DTableAdapter = new DB_Maket.ASSDataSetTableAdapters.Met_DTableAdapter(); aSSDataSetMet_DTableAdapter.Fill(aSSDataSet.Met_D); System.Windows.Data.CollectionViewSource met_DViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("met_DViewSource"))); met_DViewSource.View.MoveCurrentToFirst(); MessageBox.Show("Запись успешно удалена"); break; } catch { MessageBox.Show("Не удалось удалить запись"); break; } } case MessageBoxResult.Cancel: { break; } } }