AdduserForm.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. namespace Session_4
  12. {
  13. public partial class AdduserForm : Form
  14. {
  15. SqlConnection connection;
  16. SqlCommand command;
  17. SqlDataAdapter adapter;
  18. DataTable table;
  19. public AdduserForm()
  20. {
  21. InitializeComponent();
  22. connection = new SqlConnection("Server=Class31000;Database=Session3_4;Trusted_Connection=True;");
  23. command = new SqlCommand();
  24. command.Connection = connection;
  25. command.CommandType = CommandType.Text;
  26. adapter = new SqlDataAdapter(command);
  27. table = new DataTable();
  28. }
  29. private void AdduserForm_Load(object sender, EventArgs e)
  30. {
  31. // TODO: данная строка кода позволяет загрузить данные в таблицу "session3_4DataSet.Offices". При необходимости она может быть перемещена или удалена.
  32. this.officesTableAdapter.Fill(this.session3_4DataSet.Offices);
  33. }
  34. private void cancel_button_Click(object sender, EventArgs e)
  35. {
  36. this.Close();
  37. }
  38. private void save_button_Click(object sender, EventArgs e)
  39. {
  40. if(string.IsNullOrEmpty(email_text.Text) || string.IsNullOrEmpty(firstname_text.Text) || string.IsNullOrEmpty(lastname_text.Text) || string.IsNullOrEmpty(password_text.Text))
  41. {
  42. MessageBox.Show("Все поля должны быть заполнены!");
  43. return;
  44. }
  45. else
  46. {
  47. connection.Open();
  48. command.CommandText = "INSERT INTO Users(RoleID, Email, Password, FirstName, LastName, OfficeID, Birthdate, Active) VALUES(2, '@email', '@password', '@firstname', @lastname, @office , @date, 'False')";
  49. command.Parameters.AddWithValue("@email", email_text.Text);
  50. command.Parameters.AddWithValue("password", password_text.Text);
  51. command.Parameters.AddWithValue("@firstname", firstname_text.Text);
  52. command.Parameters.AddWithValue("@lastname", lastname_text.Text);
  53. command.Parameters.AddWithValue("@office", officebox_text.SelectedValue);
  54. command.Parameters.AddWithValue("@date", dateTimePicker1.Value);
  55. command.ExecuteReader();
  56. connection.Close();
  57. }
  58. }
  59. }
  60. }