Advertisement
annstasi

Untitled

Apr 19th, 2021
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 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 bd
  13. {
  14. public partial class Form2 : Form
  15. {
  16. public SqlConnection sql = null;
  17.  
  18. public Form2(SqlConnection sqlConnection)
  19. {
  20. InitializeComponent();
  21. // НЕ ПОМНЮ ЗАЧЕМ ЗДЕСЬ ТОЖЕ ЭТА СТРОКА, такая же внизу
  22. string ConnectionString = @"Data Source=192.168.112.103;Initial Catalog=db22204;User ID=User032;Password=PASSWORD;Integrated Security=False";
  23. sqlConnection = new SqlConnection(ConnectionString);
  24.  
  25. sqlConnection.Open();
  26. sql = sqlConnection;
  27. // У тебя такое было, просто список счетов
  28. SqlCommand command1 = new SqlCommand("SELECT txtAccountTypeName FROM tblAccountType", sqlConnection);
  29. SqlDataReader reader1 = command1.ExecuteReader();
  30. var sp1 = new List<string>();
  31. while (reader1.Read())
  32. {
  33. sp1.Add(reader1.GetString(0));
  34. }
  35. reader1.Close();
  36. txtAccountTypeName.DataSource = sp1;
  37. //СПИСОК СУЩЕСТВУЮЩИХ ФИО
  38. SqlCommand command2 = new SqlCommand("SELECT concat(txtClientSurname, ' ', txtClientName, ' ', txtClientSecondName) FROM tblClient", sqlConnection);
  39. SqlDataReader reader2 = command2.ExecuteReader();
  40. var sp2 = new List<string>();
  41. while (reader2.Read())
  42. {
  43. sp2.Add(reader2.GetString(0));
  44. }
  45. reader2.Close();
  46. Client.DataSource = sp2;
  47. sqlConnection.Close();
  48. }
  49.  
  50. private void Form2_Load(object sender, EventArgs e)
  51. {
  52.  
  53. }
  54. private void Add_Click(object sender, EventArgs e)
  55. {
  56. int idperson; // АЙДИ ДЛЯ ФИО
  57. string ConnectionString = @"Data Source=192.168.112.103;Initial Catalog=db22204;User ID=User032;Password=PASSWORD;Integrated Security=False";
  58.  
  59. if (Client.Items.Count > 0 && txtAccountTypeName.Items.Count > 0 && datAccountBegin.TextLength > 0 && txtAccountNumber.TextLength > 0 && txtAccountNumber.TextLength > 0 && fltAccountSum.TextLength > 0)
  60. {
  61. sql.Open();
  62. // СВЯЗКА ФИО ПО АЙДИ
  63. var a = Client.Text.ToString();
  64. var b = a.Split(' ');
  65. using (SqlConnection connection = new SqlConnection(ConnectionString))
  66. {
  67. connection.Open();
  68. SqlCommand sqlCommand1 = new SqlCommand("SELECT intClientId FROM tblClient WHERE (txtClientSurname = '" + b[0] + "' and txtClientName = '" + b[1] + "' and txtClientSecondName = '" + b[2] + "')", sql);
  69. idperson = (int)sqlCommand1.ExecuteScalar();
  70. sqlCommand1.ExecuteNonQuery();
  71. connection.Close();
  72. }
  73.  
  74. SqlCommand sqlCommand = new SqlCommand(
  75. $"INSERT INTO[tblAccount](intClientId, intAccountTypeId, datAccountBegin, datAccountEnd, txtAccountNumber, fltAccountSum) VALUES (@intClientId, @intAccountTypeId, @datAccountBegin, @datAccountEnd, @txtAccountNumber, @fltAccountSum)", sql
  76. );
  77. // Связка списка счетов по id, у тебя было по аналогии
  78. SqlCommand command2 = new SqlCommand("(Select tblAccountType.intAccountTypeId FROM tblAccountType WHERE txtAccountTypeName = '" + txtAccountTypeName.SelectedItem.ToString() + "') ", sql);
  79. SqlDataReader reader = command2.ExecuteReader();
  80. reader.Read();
  81. var id = reader.GetInt32(0);
  82. reader.Close();
  83.  
  84. DateTime date = DateTime.Parse(datAccountBegin.Text);
  85. DateTime dateEnd = DateTime.Parse(datAccountEnd.Text);
  86. sqlCommand.Parameters.AddWithValue("intClientId", idperson);
  87. sqlCommand.Parameters.AddWithValue("intAccountTypeId", id);
  88. sqlCommand.Parameters.AddWithValue("datAccountBegin", $"{date.Month}/{date.Day}/{date.Year}");
  89. sqlCommand.Parameters.AddWithValue("datAccountEnd", $"{dateEnd.Month}/{dateEnd.Day}/{dateEnd.Year}");
  90. sqlCommand.Parameters.AddWithValue("txtAccountNumber", txtAccountNumber.Text);
  91. sqlCommand.Parameters.AddWithValue("fltAccountSum", int.Parse(fltAccountSum.Text));
  92.  
  93. sqlCommand.ExecuteNonQuery();
  94. this.Close();
  95. }
  96. }
  97.  
  98. private void Cancel_Click_1(object sender, EventArgs e)
  99. {
  100. this.Close();
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement