Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. try
  2. {
  3. string sql = "SELECT * FROM " + textBox51.Text; // Строка запроса
  4.  
  5. MySqlConnection connection = new MySqlConnection(connStrMySql);
  6.  
  7. connection.Open();
  8.  
  9. #region *** ДОБАВЛЕНИЕ ЗАПИСИ В ТАБЛИЦУ ***
  10.  
  11. #region *** ПЕРЕМЕННАЯ ***
  12. int s; // СТАРТ. ПОЗИЦИЯ С КОТОРОЙ НУЖНО НАЧИНАТЬ ДОБАВЛЯТЬ ЗАПИСИ
  13. s = Convert.ToInt32(textBox55.Text);
  14.  
  15. int f; // ФИНИШ. ПОЗИЦИЯ НА КОТОРОЙ НУЖНО ЗАКОНЧИТЬ ДОБАВЛЯТЬ ЗАПИСИ
  16. f = Convert.ToInt32(textBox56.Text);
  17.  
  18. int r = f - s; // КОЛ. КОЛИЧЕСТВО ПОЗИЦИЙ, КОТОРОЕ ТРЕБУЕТСЯ ДОБАВИТЬ
  19.  
  20. int otkl = s + 1000;
  21. #endregion *** ПЕРЕМЕННАЯ ***. КОНЕЦ ХХХ
  22.  
  23. for (s = Convert.ToInt32(textBox55.Text); s <= f; s++) //
  24. {
  25. pol_2 = s;
  26.  
  27. int col;
  28. col = Convert.ToInt32(textBox59.Text);
  29.  
  30. for (int pol_3 = 1; pol_3 <= col; pol_3++) //
  31. {
  32.  
  33. MySqlCommand sqlCom = new MySqlCommand
  34. ("INSERT INTO " + textBox51.Text + // ТАБЛИЦА
  35. "(" + textBox52.Text + // ПОЛЯ
  36. ") VALUES"
  37. + "('"
  38. +
  39. pol_1 + "', '" + pol_2 + "', '" + pol_3 + "', '" + pol_4 + "', '" + pol_5 + "', '" + pol_6 + "', '" + pol_7 + "', '" + pol_8 + "', '" + pol_9 + "', '" + pol_10 + "', '" + pol_11 + "', '" + pol_12 + "', '" + pol_13 + "', '" + pol_14 + "', '" + pol_15 + "', '" + pol_16 + "', '" + pol_17
  40. +
  41. "')",
  42. connection); // РАБОТАЕТ. ПОДСТАВЛЯЕТСЯ значение из переменных
  43. sqlCom.ExecuteNonQuery();
  44. }
  45.  
  46. if (s == otkl)
  47. {
  48. connection.Close();
  49. connection.Open();
  50. otkl = s + 1000;
  51. }
  52.  
  53. }
  54. MessageBox.Show("Добавлено " + r + " записей ");
  55. return;
  56.  
  57. }
  58. catch (Exception ex)
  59. {
  60. richTextBox1.Text += "!" + "rn";
  61. richTextBox1.Text += "ОШИБКА" + "rn";
  62.  
  63. textBox1.AppendText("! " + "rn");
  64. textBox1.AppendText("ОШИБКА ХХХ " + "rn");
  65.  
  66. MessageBox.Show(ex.ToString());
  67. MessageBox.Show("ОШИБКА ХХХ ");
  68. return;
  69. }
  70.  
  71. private void insertData()
  72. {
  73. string conStr = "server=127.0.0.1;user=root;" +
  74. "database=test;password=123;";
  75. using (MySqlConnection con = new MySqlConnection(conStr))
  76. {
  77. try
  78. {
  79. string sql = "INSERT INTO friends (name, lastname, age)" +
  80. "VALUES (@Name, @LastName, @Age);";
  81. con.Open();
  82. MySqlCommand cmd = new MySqlCommand(sql, con);
  83. //создаем параметры и добавляем их в коллекцию
  84. cmd.Parameters.AddWithValue("@Name", "Vasya");
  85. cmd.Parameters.AddWithValue("@LastName", "Pupkin");
  86. cmd.Parameters.AddWithValue("@Age", 18);
  87. cmd.ExecuteNonQuery();
  88. }
  89. catch (Exception ex)
  90. {
  91. MessageBox.Show(ex.Message);
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement