Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using MySql.Data.MySqlClient;
- namespace WindowsFormsApplication1
- {
- public partial class Form2 : Form
- {
- string conn = "SERVER=192.168.1.113;DATABASE=db_A001;UID=user_A001;PASSWORD=password_A001;";
- MySqlDataAdapter dataAdapter;
- public Form2()
- {
- InitializeComponent();
- GetData("SELECT * FROM Users");
- }
- void GetData(string selectCommand)
- {
- MySqlConnection connection = new MySqlConnection(conn);
- connection.Open();
- dataAdapter = new MySqlDataAdapter(selectCommand, conn);
- MySqlCommandBuilder comBuild = new MySqlCommandBuilder(dataAdapter);
- DataTable table = new DataTable();
- dataAdapter.Fill(table);
- usersBindingSource.DataSource = table;
- dataGridView2.DataSource = usersBindingSource;
- }
- private void Form2_FormClosed(object sender, FormClosedEventArgs e)
- {
- var form1 = new Form1();
- form1.Show();
- }
- private void Form2_Load(object sender, EventArgs e)
- {
- MySqlConnection connection = new MySqlConnection(conn);
- connection.Open();
- }
- private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- //usersBindingSource.EndEdit();
- DataTable dt = (DataTable)usersBindingSource.DataSource;
- dataAdapter.Update(dt);
- MessageBox.Show("Information Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
- GetData("SELECT * FROM Users");
- //dataAdapter = new MySqlDataAdapter("SELECT * FROM Users", conn);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- //private void button2_Click(object sender, EventArgs e)
- //{
- /* try
- {
- MySqlConnection connection = new MySqlConnection(conn);
- connection.Open();
- string Query = "SELECT * FROM Users;";
- MySqlCommand newcom = new MySqlCommand(Query, connection);
- MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
- MyAdapter.SelectCommand = newcom;
- DataTable dTable = new DataTable();
- MyAdapter.Fill(dTable);
- dataGridView2.DataSource = dTable;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }*/
- //}
- private void button3_Click(object sender, EventArgs e)
- {
- MySqlConnection connection = new MySqlConnection(conn);
- connection.Open();
- string query = "DELETE FROM Users WHERE Username= '" + textBox1.Text + "';";
- MySqlCommand cmd = new MySqlCommand(query, connection);
- cmd.ExecuteNonQuery();
- GetData("SELECT * FROM Users");
- }
- private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement