Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 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.Windows.Forms;
  9. using MySql.Data.MySqlClient;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13. public partial class Form2 : Form
  14. {
  15. string conn = "SERVER=192.168.1.113;DATABASE=db_A001;UID=user_A001;PASSWORD=password_A001;";
  16. MySqlDataAdapter dataAdapter;
  17. public Form2()
  18. {
  19. InitializeComponent();
  20.  
  21. GetData("SELECT * FROM Users");
  22.  
  23. }
  24. void GetData(string selectCommand)
  25. {
  26.  
  27. MySqlConnection connection = new MySqlConnection(conn);
  28. connection.Open();
  29.  
  30.  
  31. dataAdapter = new MySqlDataAdapter(selectCommand, conn);
  32. MySqlCommandBuilder comBuild = new MySqlCommandBuilder(dataAdapter);
  33. DataTable table = new DataTable();
  34. dataAdapter.Fill(table);
  35. usersBindingSource.DataSource = table;
  36. dataGridView2.DataSource = usersBindingSource;
  37.  
  38. }
  39. private void Form2_FormClosed(object sender, FormClosedEventArgs e)
  40. {
  41. var form1 = new Form1();
  42. form1.Show();
  43. }
  44.  
  45. private void Form2_Load(object sender, EventArgs e)
  46. {
  47.  
  48. MySqlConnection connection = new MySqlConnection(conn);
  49. connection.Open();
  50.  
  51.  
  52.  
  53. }
  54.  
  55. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  56. {
  57.  
  58. }
  59.  
  60. private void button1_Click(object sender, EventArgs e)
  61. {
  62. try
  63. {
  64. //usersBindingSource.EndEdit();
  65. DataTable dt = (DataTable)usersBindingSource.DataSource;
  66.  
  67. dataAdapter.Update(dt);
  68. MessageBox.Show("Information Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
  69. GetData("SELECT * FROM Users");
  70. //dataAdapter = new MySqlDataAdapter("SELECT * FROM Users", conn);
  71. }
  72. catch (Exception ex)
  73. {
  74. MessageBox.Show(ex.Message);
  75.  
  76. }
  77.  
  78. }
  79.  
  80. //private void button2_Click(object sender, EventArgs e)
  81. //{
  82. /* try
  83. {
  84.  
  85. MySqlConnection connection = new MySqlConnection(conn);
  86. connection.Open();
  87. string Query = "SELECT * FROM Users;";
  88. MySqlCommand newcom = new MySqlCommand(Query, connection);
  89. MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
  90. MyAdapter.SelectCommand = newcom;
  91. DataTable dTable = new DataTable();
  92. MyAdapter.Fill(dTable);
  93. dataGridView2.DataSource = dTable;
  94.  
  95. }
  96. catch (Exception ex)
  97. {
  98. MessageBox.Show(ex.Message);
  99.  
  100. }*/
  101.  
  102. //}
  103.  
  104. private void button3_Click(object sender, EventArgs e)
  105. {
  106. MySqlConnection connection = new MySqlConnection(conn);
  107. connection.Open();
  108. string query = "DELETE FROM Users WHERE Username= '" + textBox1.Text + "';";
  109.  
  110.  
  111.  
  112. MySqlCommand cmd = new MySqlCommand(query, connection);
  113. cmd.ExecuteNonQuery();
  114. GetData("SELECT * FROM Users");
  115.  
  116. }
  117.  
  118. private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
  119. {
  120.  
  121. }
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement