Advertisement
Guest User

ADO-db-test

a guest
Feb 25th, 2012
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. namespace ADODatabases {
  2.     /// <summary>
  3.     /// Interaction logic for MainWindow.xaml
  4.     /// </summary>
  5.     public partial class MainWindow : Window {
  6.         public MainWindow() {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private void Window_Loaded(object sender, RoutedEventArgs e) {
  11.             string connStr = @"Data Source=hansen-laptop\SQLEXPRESS;
  12.          database=Northwind;Integrated Security=True";
  13.            
  14.             SqlConnection con = new SqlConnection(connStr);
  15.             string sql = "SELECT distinct country FROM customers";
  16.            
  17.             SqlCommand cmd = new SqlCommand(sql, con);
  18.             con.Open();
  19.            
  20.             SqlDataReader reader = cmd.ExecuteReader();
  21.             while (reader.Read()) {
  22.                 string line ="" + reader[0];
  23.                 comboBox1.Items.Add(line);
  24.             }
  25.             con.Close();
  26.  
  27.         }
  28.  
  29.         private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) {
  30.             string connStr = @"Data Source=hansen-laptop\SQLEXPRESS;
  31.          database=Northwind;Integrated Security=True";
  32.            
  33.             SqlConnection con = new SqlConnection(connStr);
  34.             string sql = "SELECT ContactName, City FROM customers WHERE country=@country";
  35.            
  36.             SqlCommand cmd = new SqlCommand(sql, con);
  37.  
  38.             cmd.Parameters.AddWithValue("@country", comboBox1.SelectedValue.ToString());
  39.  
  40.             con.Open();
  41.            
  42.             SqlDataReader reader = cmd.ExecuteReader();
  43.             listBox1.Items.Clear();
  44.             while (reader.Read()) {
  45.                 string line = "" + reader[0];
  46.                 listBox1.Items.Add(line);
  47.             }
  48.             con.Close();
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement