Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 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 System.Data.SqlClient;
  10.  
  11. using MySql.Data.MySqlClient;
  12.  
  13. namespace WindowsFormsApplication1 {
  14.  
  15. public partial class Form1 : Form {
  16.  
  17. private static string connStr = "server=localhost;user=root;database=winestore;port=3306;";
  18. private static MySqlConnection connection = new MySqlConnection(connStr);
  19.  
  20. public Form1() {
  21. try {
  22. InitializeComponent();
  23. connection.Open();
  24.  
  25. MySqlDataAdapter adapter = new MySqlDataAdapter();
  26. DataSet ds = new DataSet();
  27. string commandText = "SELECT winery_name FROM winery;";
  28. adapter.SelectCommand = new MySqlCommand(commandText, connection);
  29. adapter.Fill(ds, "winery_name");
  30.  
  31. comboBox1.DisplayMember = "winery_name";
  32. comboBox1.ValueMember = "winery_id";
  33. comboBox1.DataSource = ds.Tables["winery_name"];
  34. } catch (Exception ex) {
  35. MessageBox.Show(ex.ToString());
  36. }
  37. }
  38.  
  39. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
  40. MySqlDataAdapter adapter = new MySqlDataAdapter();
  41. DataTable table = new DataTable();
  42. string commandText = "SELECT wine_name, year FROM wine WHERE winery_id='" + comboBox1.SelectedValue + "';";
  43. adapter.SelectCommand = new MySqlCommand(commandText, connection);
  44. adapter.Fill(table);
  45. MessageBox.Show(comboBox1.SelectedValue.ToString());
  46. dataGridView1.DataSource = table;
  47. dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
  48. }
  49.  
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement