Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace WindowsFormsApp9
- {
- public partial class Form1 : Form
- {
- string connectionString = Properties.Settings.Default.RegistraturaConnectionString;
- SqlConnection connection;
- private SqlDataAdapter sqlDataAdapter;
- private DataSet ds;
- private BindingSource bs;
- public string[] getData;
- public Form1()
- {
- InitializeComponent();
- connection = new SqlConnection(connectionString);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- using (connection)
- {
- connection.Open();
- DataTable tables = connection.GetSchema("Tables");
- foreach (DataRow row in tables.Rows)
- {
- string TableName = row["TABLE_NAME"].ToString();
- toolStripComboBox1.Items.Add(TableName);
- }
- }
- toolStripComboBox1.SelectedItem = toolStripComboBox1.Items[0].ToString();
- LoadData();
- }
- private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- using (connection = new SqlConnection(connectionString))
- {
- string sTable = toolStripComboBox1.SelectedItem.ToString();
- sqlDataAdapter = new SqlDataAdapter("SELECT * FROM " + sTable, connection);
- ds = new DataSet();
- bs = new BindingSource();
- connection.Open();
- sqlDataAdapter.Fill(ds);
- bs.DataSource = ds.Tables[0];
- bindingNavigator1.BindingSource = bs;
- dataGridView1.DataSource = bs;
- connection.Close();
- }
- dataGridView1.AutoResizeColumns();
- dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
- }
- private void addButton_Click(object sender, EventArgs e)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- string command = tbCom.Text;
- sqlDataAdapter = new SqlDataAdapter(command, connection);
- ds = new DataSet();
- bs = new BindingSource();
- int index = toolStripComboBox1.SelectedIndex;
- connection.Open();
- sqlDataAdapter.Fill(ds);
- bs.DataSource = ds.Tables[index];
- dataGridView1.DataSource = bs;
- connection.Close();
- }
- }
- private void addButton_Click_1(object sender, EventArgs e)
- {
- addPanel.Visible = true;
- button1.Visible = true;
- int n = dataGridView1.ColumnCount;
- if (toolStripComboBox1.SelectedItem.ToString() == "Doctors" || toolStripComboBox1.SelectedItem.ToString() == "Patients")
- {
- rtb1.Visible = true;
- rtb2.Visible = true;
- rtb3.Visible = true;
- rtb4.Visible = true;
- rtb5.Visible = false;
- rtb6.Visible = false;
- text1.Visible = true;
- text2.Visible = true;
- text3.Visible = true;
- text4.Visible = true;
- text5.Visible = false;
- text6.Visible = false;
- text1.Text = dataGridView1.Columns[0].Name.ToString();
- text2.Text = dataGridView1.Columns[1].Name.ToString();
- text3.Text = dataGridView1.Columns[2].Name.ToString();
- text4.Text = dataGridView1.Columns[3].Name.ToString();
- }
- else
- {
- rtb1.Visible = true;
- rtb2.Visible = true;
- rtb3.Visible = true;
- rtb4.Visible = true;
- rtb5.Visible = true;
- rtb6.Visible = true;
- text1.Visible = true;
- text2.Visible = true;
- text3.Visible = true;
- text4.Visible = true;
- text5.Visible = true;
- text6.Visible = true;
- text1.Text = dataGridView1.Columns[0].Name.ToString();
- text2.Text = dataGridView1.Columns[1].Name.ToString();
- text3.Text = dataGridView1.Columns[2].Name.ToString();
- text4.Text = dataGridView1.Columns[3].Name.ToString();
- text5.Text = dataGridView1.Columns[4].Name.ToString();
- text6.Text = dataGridView1.Columns[5].Name.ToString();
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- int n = dataGridView1.ColumnCount;
- string addComText;
- addComText = String.Format("INSERT INTO " + toolStripComboBox1.SelectedItem + " (");
- for (int i = 0; i < n; i++)
- {
- addComText = addComText + "[" + dataGridView1.Columns[i].Name.ToString() + "]";
- if (i < n - 1) addComText = addComText + ",";
- }
- addComText = addComText + ") VALUES (";
- for (int i = 0; i < n; i++)
- {
- addComText = addComText + "'{" + i + "}'";
- if (i < n - 1)
- addComText = addComText + ", ";
- }
- if (n == 4)
- addComText = String.Format(addComText + ") ", rtb1.Text, rtb2.Text, rtb3.Text, rtb4.Text);
- if (n == 6)
- addComText = String.Format(addComText + ") ", rtb1.Text, rtb2.Text, rtb3.Text, rtb4.Text, rtb5.Text, rtb6.Text);
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- SqlCommand command = new SqlCommand(addComText, connection);
- connection.Open();
- try
- {
- command.ExecuteNonQuery();
- }
- catch (SqlException)
- {
- MessageBox.Show("Проверьте корректность введённых данных.");
- }
- connection.Close();
- }
- addPanel.Visible = false;
- button1.Visible = false;
- LoadData();
- }
- private void delBut_Click(object sender, EventArgs e)
- {
- if (MessageBox.Show("Вы действительно хотите удалить выбранную запись?", "Удаление", MessageBoxButtons.YesNo) == DialogResult.No)
- return;
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- string currRow = dataGridView1.CurrentRow.Cells[0].Value.ToString();
- string sqlText = "DELETE FROM " + toolStripComboBox1.SelectedItem + " WHERE [" + dataGridView1.Columns[0].Name.ToString() + "] = " + currRow;
- MessageBox.Show(sqlText);
- SqlCommand command = new SqlCommand(sqlText, connection);
- connection.Open();
- command.ExecuteNonQuery();
- connection.Close();
- }
- LoadData();
- }
- private void LoadData()
- {
- using (connection = new SqlConnection(connectionString))
- {
- string nowTable = toolStripComboBox1.SelectedItem.ToString();
- sqlDataAdapter = new SqlDataAdapter("SELECT * FROM " + nowTable, connection);
- ds = new DataSet();
- bs = new BindingSource();
- connection.Open();
- sqlDataAdapter.Fill(ds);
- bs.DataSource = ds.Tables[0];
- dataGridView1.DataSource = bs;
- connection.Close();
- }
- }
- private void editButton_Click(object sender, EventArgs e)
- {
- addPanel.Visible = true;
- finishEditBut.Visible = true;
- int n = dataGridView1.ColumnCount;
- if (toolStripComboBox1.SelectedItem.ToString() == "Doctors" || toolStripComboBox1.SelectedItem.ToString() == "Patients")
- {
- rtb1.Visible = true;
- rtb2.Visible = true;
- rtb3.Visible = true;
- rtb4.Visible = true;
- rtb5.Visible = false;
- rtb6.Visible = false;
- text1.Visible = true;
- text2.Visible = true;
- text3.Visible = true;
- text4.Visible = true;
- text5.Visible = false;
- text6.Visible = false;
- text1.Text = dataGridView1.Columns[0].Name.ToString();
- text2.Text = dataGridView1.Columns[1].Name.ToString();
- text3.Text = dataGridView1.Columns[2].Name.ToString();
- text4.Text = dataGridView1.Columns[3].Name.ToString();
- try
- {
- rtb1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
- rtb2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
- rtb3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
- rtb4.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
- }
- catch { }
- }
- else
- {
- rtb1.Visible = true;
- rtb2.Visible = true;
- rtb3.Visible = true;
- rtb4.Visible = true;
- rtb5.Visible = true;
- rtb6.Visible = true;
- text1.Visible = true;
- text2.Visible = true;
- text3.Visible = true;
- text4.Visible = true;
- text5.Visible = true;
- text6.Visible = true;
- text1.Text = dataGridView1.Columns[0].Name.ToString();
- text2.Text = dataGridView1.Columns[1].Name.ToString();
- text3.Text = dataGridView1.Columns[2].Name.ToString();
- text4.Text = dataGridView1.Columns[3].Name.ToString();
- text5.Text = dataGridView1.Columns[4].Name.ToString();
- text6.Text = dataGridView1.Columns[5].Name.ToString();
- try
- {
- rtb1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
- rtb2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
- rtb3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
- rtb4.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
- rtb5.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
- rtb6.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
- }
- catch { }
- }
- }
- private void finishEditBut_Click(object sender, EventArgs e)
- {
- int n = dataGridView1.ColumnCount;
- string addComText;
- addComText = String.Format("UPDATE " + toolStripComboBox1.SelectedItem + " SET ");
- for (int i = 0; i < n; i++)
- {
- addComText = addComText + dataGridView1.Columns[i].Name.ToString() +" = '";
- addComText = addComText + "{" + i + "}', ";
- }
- addComText = addComText.Substring(0, addComText.Length - 2);
- addComText = addComText + " WHERE " + dataGridView1.Columns[0].Name.ToString() + " = " + dataGridView1.CurrentRow.Cells[0].Value.ToString();
- if (n == 4)
- addComText = String.Format(addComText, rtb1.Text, rtb2.Text, rtb3.Text, rtb4.Text);
- if (n == 6)
- addComText = String.Format(addComText, rtb1.Text, rtb2.Text, rtb3.Text, rtb4.Text, rtb5.Text, rtb6.Text);
- MessageBox.Show(addComText);
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- SqlCommand command = new SqlCommand(addComText, connection);
- connection.Open();
- try
- {
- command.ExecuteNonQuery();
- }
- catch (SqlException)
- {
- MessageBox.Show("Проверьте корректность введённых данных.");
- }
- connection.Close();
- }
- addPanel.Visible = false;
- finishEditBut.Visible = false;
- LoadData();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement