Advertisement
Guest User

Untitled

a guest
May 27th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 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.  
  13. namespace WindowsFormsApplication1
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. private void Form1_Load(object sender, EventArgs e)
  23. {
  24.  
  25. }
  26.  
  27. private void addbutton_Click(object sender, EventArgs e)
  28. {
  29.  
  30.  
  31.  
  32. try
  33. {
  34.  
  35. String Query = string.Format("INSERT INTO dbo.Kierunek (nazwa_kierunku,wydział_id) VALUES ('{0}','{1}')", nazwakierunkutxt.Text, wydzialidtxt.Text);
  36.  
  37.  
  38. SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
  39. builder.DataSource = "172.30.52.237,1500";
  40.  
  41. builder.UserID = "sa";
  42. builder.Password = "Matis123";
  43. builder.InitialCatalog = "Uczelnia2";
  44. using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
  45. {
  46.  
  47.  
  48.  
  49. SqlCommand Command = new SqlCommand(Query, connection);
  50.  
  51.  
  52. Command.Connection.Open();
  53. Command.ExecuteNonQuery();
  54.  
  55. MessageBox.Show("Saved");
  56.  
  57. connection.Close();
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. }
  68. catch (SqlException ex)
  69. {
  70. MessageBox.Show(ex.Message);
  71. }
  72. }
  73.  
  74. private void Delete_Click(object sender, EventArgs e)
  75. {
  76. try
  77. {
  78.  
  79. String Query = string.Format("DELETE FROM dbo.Kierunek WHERE (id_kierunku='{0}')", id_kierunkutxt.Text);
  80.  
  81.  
  82. SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
  83. builder.DataSource = "172.30.52.237,1500";
  84.  
  85. builder.UserID = "sa";
  86. builder.Password = "Matis123";
  87. builder.InitialCatalog = "Uczelnia2";
  88. using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
  89. {
  90.  
  91.  
  92.  
  93. SqlCommand Command = new SqlCommand(Query, connection);
  94.  
  95.  
  96. Command.Connection.Open();
  97. Command.ExecuteNonQuery();
  98.  
  99. MessageBox.Show("Deleted");
  100.  
  101. connection.Close();
  102. }
  103.  
  104. }
  105. catch (SqlException ex)
  106. {
  107. MessageBox.Show(ex.Message);
  108. }
  109. }
  110.  
  111. private void Select_Click(object sender, EventArgs e)
  112. {
  113. try
  114. {
  115.  
  116. String Query = string.Format("SELECT nazwa_kierunku, wydział_id FROM dbo.Kierunek WHERE (id_kierunku='{0}')", id_kierunkutxt.Text);
  117.  
  118.  
  119. SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
  120. builder.DataSource = "172.30.52.237,1500";
  121.  
  122. builder.UserID = "sa";
  123. builder.Password = "Matis123";
  124. builder.InitialCatalog = "Uczelnia2";
  125. using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
  126. {
  127.  
  128.  
  129.  
  130. SqlCommand Command = new SqlCommand(Query, connection);
  131. Command.CommandType = CommandType.Text;
  132. Command.Connection.Open();
  133. SqlDataReader dr = Command.ExecuteReader();
  134. if (dr.Read())
  135. {
  136. //id_kierunkutxt.Text = dr[1].ToString();
  137. nazwakierunkutxt.Text = dr[0].ToString();
  138. wydzialidtxt.Text = dr[1].ToString();
  139. }
  140. dr.Close();
  141. }
  142.  
  143.  
  144.  
  145. }
  146.  
  147.  
  148. catch (SqlException ex)
  149. {
  150. MessageBox.Show(ex.Message);
  151. }
  152. }
  153.  
  154. private void Update_Click(object sender, EventArgs e)
  155. {
  156. try
  157. {
  158.  
  159. String Query = string.Format("UPDATE dbo.Kierunek SET nazwa_kierunku='{0}',wydział_id='{1}' WHERE id_kierunku='{2}'",nazwakierunkutxt.Text,wydzialidtxt.Text, id_kierunkutxt.Text);
  160.  
  161.  
  162. SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
  163. builder.DataSource = "172.30.52.237,1500";
  164.  
  165. builder.UserID = "sa";
  166. builder.Password = "Matis123";
  167. builder.InitialCatalog = "Uczelnia2";
  168. using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
  169. {
  170.  
  171.  
  172.  
  173. SqlCommand Command = new SqlCommand(Query, connection);
  174.  
  175.  
  176. Command.Connection.Open();
  177. Command.ExecuteNonQuery();
  178.  
  179. MessageBox.Show("Updated");
  180.  
  181. connection.Close();
  182. }
  183.  
  184. }
  185. catch (SqlException ex)
  186. {
  187. MessageBox.Show(ex.Message);
  188. }
  189. }
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement