Advertisement
Guest User

kurs

a guest
Mar 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11.  
  12. namespace WindowsFormsApp9
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         string connectionString = Properties.Settings.Default.RegistraturaConnectionString;
  17.         SqlConnection connection;
  18.         private SqlDataAdapter sqlDataAdapter;
  19.         private DataSet ds;
  20.         private BindingSource bs;
  21.         public string[] getData;
  22.  
  23.  
  24.         public Form1()
  25.         {
  26.             InitializeComponent();
  27.             connection = new SqlConnection(connectionString);
  28.         }
  29.  
  30.         private void Form1_Load(object sender, EventArgs e)
  31.         {
  32.  
  33.             using (connection)
  34.             {
  35.                 connection.Open();
  36.  
  37.                 DataTable tables = connection.GetSchema("Tables");
  38.                 foreach (DataRow row in tables.Rows)
  39.                 {
  40.                     string TableName = row["TABLE_NAME"].ToString();
  41.                     toolStripComboBox1.Items.Add(TableName);
  42.                 }
  43.             }
  44.             toolStripComboBox1.SelectedItem = toolStripComboBox1.Items[0].ToString();
  45.             LoadData();
  46.  
  47.  
  48.         }
  49.  
  50.         private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
  51.         {
  52.             using (connection = new SqlConnection(connectionString))
  53.             {
  54.                 string sTable = toolStripComboBox1.SelectedItem.ToString();
  55.                 sqlDataAdapter = new SqlDataAdapter("SELECT * FROM " + sTable, connection);
  56.                 ds = new DataSet();
  57.                 bs = new BindingSource();
  58.  
  59.                 connection.Open();
  60.  
  61.                 sqlDataAdapter.Fill(ds);
  62.                 bs.DataSource = ds.Tables[0];
  63.                 bindingNavigator1.BindingSource = bs;
  64.                 dataGridView1.DataSource = bs;
  65.  
  66.                 connection.Close();
  67.             }
  68.             dataGridView1.AutoResizeColumns();
  69.             dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  70.         }
  71.  
  72.         private void addButton_Click(object sender, EventArgs e)
  73.         {
  74.             using (SqlConnection connection = new SqlConnection(connectionString))
  75.             {
  76.                 string command = tbCom.Text;
  77.                 sqlDataAdapter = new SqlDataAdapter(command, connection);
  78.                 ds = new DataSet();
  79.                 bs = new BindingSource();
  80.                 int index = toolStripComboBox1.SelectedIndex;
  81.                 connection.Open();
  82.                 sqlDataAdapter.Fill(ds);
  83.                 bs.DataSource = ds.Tables[index];
  84.                 dataGridView1.DataSource = bs;
  85.                 connection.Close();
  86.             }
  87.  
  88.         }
  89.  
  90.         private void addButton_Click_1(object sender, EventArgs e)
  91.         {
  92.             addPanel.Visible = true;
  93.             button1.Visible = true;
  94.             int n = dataGridView1.ColumnCount;            
  95.             if (toolStripComboBox1.SelectedItem.ToString() == "Doctors" || toolStripComboBox1.SelectedItem.ToString() == "Patients")
  96.             {
  97.                 rtb1.Visible = true;
  98.                 rtb2.Visible = true;
  99.                 rtb3.Visible = true;
  100.                 rtb4.Visible = true;
  101.                 rtb5.Visible = false;
  102.                 rtb6.Visible = false;
  103.                 text1.Visible = true;
  104.                 text2.Visible = true;
  105.                 text3.Visible = true;
  106.                 text4.Visible = true;
  107.                 text5.Visible = false;
  108.                 text6.Visible = false;
  109.                 text1.Text = dataGridView1.Columns[0].Name.ToString();
  110.                 text2.Text = dataGridView1.Columns[1].Name.ToString();
  111.                 text3.Text = dataGridView1.Columns[2].Name.ToString();
  112.                 text4.Text = dataGridView1.Columns[3].Name.ToString();
  113.             }
  114.             else
  115.             {
  116.                 rtb1.Visible = true;
  117.                 rtb2.Visible = true;
  118.                 rtb3.Visible = true;
  119.                 rtb4.Visible = true;
  120.                 rtb5.Visible = true;
  121.                 rtb6.Visible = true;
  122.                 text1.Visible = true;
  123.                 text2.Visible = true;
  124.                 text3.Visible = true;
  125.                 text4.Visible = true;
  126.                 text5.Visible = true;
  127.                 text6.Visible = true;
  128.                 text1.Text = dataGridView1.Columns[0].Name.ToString();
  129.                 text2.Text = dataGridView1.Columns[1].Name.ToString();
  130.                 text3.Text = dataGridView1.Columns[2].Name.ToString();
  131.                 text4.Text = dataGridView1.Columns[3].Name.ToString();
  132.                 text5.Text = dataGridView1.Columns[4].Name.ToString();
  133.                 text6.Text = dataGridView1.Columns[5].Name.ToString();
  134.             }
  135.            
  136.            
  137.         }
  138.  
  139.         private void button1_Click(object sender, EventArgs e)
  140.         {
  141.             int n = dataGridView1.ColumnCount;
  142.             string addComText;
  143.             addComText = String.Format("INSERT INTO " + toolStripComboBox1.SelectedItem + " (");
  144.             for (int i = 0; i < n; i++)
  145.             {
  146.                 addComText = addComText + "[" + dataGridView1.Columns[i].Name.ToString() + "]";
  147.                 if (i < n - 1) addComText = addComText + ",";
  148.             }
  149.             addComText = addComText + ") VALUES (";
  150.             for (int i = 0; i < n; i++)
  151.             {
  152.                 addComText = addComText + "'{" + i + "}'";
  153.                 if (i < n - 1)
  154.                     addComText = addComText + ", ";
  155.             }
  156.             if (n == 4)
  157.                 addComText = String.Format(addComText + ") ", rtb1.Text, rtb2.Text, rtb3.Text, rtb4.Text);
  158.             if (n == 6)
  159.                 addComText = String.Format(addComText + ") ", rtb1.Text, rtb2.Text, rtb3.Text, rtb4.Text, rtb5.Text, rtb6.Text);
  160.             using (SqlConnection connection = new SqlConnection(connectionString))
  161.             {
  162.                 SqlCommand command = new SqlCommand(addComText, connection);
  163.                 connection.Open();
  164.                 try
  165.                 {
  166.                     command.ExecuteNonQuery();
  167.                 }
  168.                 catch (SqlException)
  169.                 {
  170.                     MessageBox.Show("Проверьте корректность введённых данных.");
  171.                 }
  172.                 connection.Close();
  173.             }
  174.             addPanel.Visible = false;
  175.             button1.Visible = false;
  176.             LoadData();
  177.         }
  178.  
  179.         private void delBut_Click(object sender, EventArgs e)
  180.         {
  181.             if (MessageBox.Show("Вы действительно хотите удалить выбранную запись?", "Удаление", MessageBoxButtons.YesNo) == DialogResult.No)
  182.                 return;
  183.             using (SqlConnection connection = new SqlConnection(connectionString))
  184.             {
  185.                 string currRow = dataGridView1.CurrentRow.Cells[0].Value.ToString();
  186.                 string sqlText = "DELETE FROM " + toolStripComboBox1.SelectedItem + " WHERE [" + dataGridView1.Columns[0].Name.ToString() + "] = " + currRow;
  187.                 MessageBox.Show(sqlText);
  188.                 SqlCommand command = new SqlCommand(sqlText, connection);
  189.                 connection.Open();
  190.                 command.ExecuteNonQuery();
  191.                 connection.Close();
  192.             }
  193.             LoadData();
  194.         }
  195.  
  196.         private void LoadData()
  197.         {
  198.             using (connection = new SqlConnection(connectionString))
  199.             {
  200.                 string nowTable = toolStripComboBox1.SelectedItem.ToString();
  201.                 sqlDataAdapter = new SqlDataAdapter("SELECT * FROM " + nowTable, connection);
  202.                 ds = new DataSet();
  203.                 bs = new BindingSource();
  204.                 connection.Open();
  205.                 sqlDataAdapter.Fill(ds);
  206.                 bs.DataSource = ds.Tables[0];
  207.                 dataGridView1.DataSource = bs;
  208.                 connection.Close();
  209.             }
  210.         }
  211.  
  212.         private void editButton_Click(object sender, EventArgs e)
  213.         {
  214.             addPanel.Visible = true;
  215.             finishEditBut.Visible = true;
  216.             int n = dataGridView1.ColumnCount;
  217.             if (toolStripComboBox1.SelectedItem.ToString() == "Doctors" || toolStripComboBox1.SelectedItem.ToString() == "Patients")
  218.             {
  219.                 rtb1.Visible = true;
  220.                 rtb2.Visible = true;
  221.                 rtb3.Visible = true;
  222.                 rtb4.Visible = true;
  223.                 rtb5.Visible = false;
  224.                 rtb6.Visible = false;
  225.                 text1.Visible = true;
  226.                 text2.Visible = true;
  227.                 text3.Visible = true;
  228.                 text4.Visible = true;
  229.                 text5.Visible = false;
  230.                 text6.Visible = false;
  231.                 text1.Text = dataGridView1.Columns[0].Name.ToString();
  232.                 text2.Text = dataGridView1.Columns[1].Name.ToString();
  233.                 text3.Text = dataGridView1.Columns[2].Name.ToString();
  234.                 text4.Text = dataGridView1.Columns[3].Name.ToString();
  235.                 try
  236.                 {
  237.                     rtb1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
  238.                     rtb2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
  239.                     rtb3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
  240.                     rtb4.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
  241.                 }
  242.                 catch { }
  243.  
  244.             }
  245.             else
  246.             {
  247.                 rtb1.Visible = true;
  248.                 rtb2.Visible = true;
  249.                 rtb3.Visible = true;
  250.                 rtb4.Visible = true;
  251.                 rtb5.Visible = true;
  252.                 rtb6.Visible = true;
  253.                 text1.Visible = true;
  254.                 text2.Visible = true;
  255.                 text3.Visible = true;
  256.                 text4.Visible = true;
  257.                 text5.Visible = true;
  258.                 text6.Visible = true;
  259.                 text1.Text = dataGridView1.Columns[0].Name.ToString();
  260.                 text2.Text = dataGridView1.Columns[1].Name.ToString();
  261.                 text3.Text = dataGridView1.Columns[2].Name.ToString();
  262.                 text4.Text = dataGridView1.Columns[3].Name.ToString();
  263.                 text5.Text = dataGridView1.Columns[4].Name.ToString();
  264.                 text6.Text = dataGridView1.Columns[5].Name.ToString();
  265.                 try
  266.                 {
  267.                     rtb1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
  268.                     rtb2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
  269.                     rtb3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
  270.                     rtb4.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
  271.                     rtb5.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
  272.                     rtb6.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
  273.                 }
  274.                 catch { }
  275.             }
  276.            
  277.         }
  278.  
  279.         private void finishEditBut_Click(object sender, EventArgs e)
  280.         {
  281.             int n = dataGridView1.ColumnCount;
  282.             string addComText;
  283.             addComText = String.Format("UPDATE " + toolStripComboBox1.SelectedItem + " SET ");
  284.             for (int i = 0; i < n; i++)
  285.             {
  286.                 addComText = addComText + dataGridView1.Columns[i].Name.ToString() +" = '";
  287.                 addComText = addComText + "{" + i + "}', ";
  288.             }
  289.             addComText = addComText.Substring(0, addComText.Length - 2);
  290.             addComText = addComText + " WHERE " + dataGridView1.Columns[0].Name.ToString() + " = " + dataGridView1.CurrentRow.Cells[0].Value.ToString();
  291.             if (n == 4)
  292.                 addComText = String.Format(addComText, rtb1.Text, rtb2.Text, rtb3.Text, rtb4.Text);
  293.             if (n == 6)
  294.                 addComText = String.Format(addComText, rtb1.Text, rtb2.Text, rtb3.Text, rtb4.Text, rtb5.Text, rtb6.Text);
  295.             MessageBox.Show(addComText);
  296.             using (SqlConnection connection = new SqlConnection(connectionString))
  297.             {
  298.                 SqlCommand command = new SqlCommand(addComText, connection);
  299.                 connection.Open();
  300.                 try
  301.                 {
  302.                     command.ExecuteNonQuery();
  303.                 }
  304.                 catch (SqlException)
  305.                 {
  306.                     MessageBox.Show("Проверьте корректность введённых данных.");
  307.                 }
  308.                 connection.Close();
  309.             }
  310.             addPanel.Visible = false;
  311.             finishEditBut.Visible = false;
  312.             LoadData();
  313.  
  314.         }
  315.     }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement