using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Data.OleDb; using System.Data; using System.Data.SqlClient; namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } public void dbCC(){ // 1. Instantiate the connection SqlConnection conn = new SqlConnection( "Data Source=(local);Initial Catalog=Northwind"); SqlDataReader rdr = null; try { // 2. Open the connection conn.Open(); // 3. Pass the connection to a command object SqlCommand cmd = new SqlCommand("select * from Customers", conn); // // 4. Use the connection // // get query results rdr = cmd.ExecuteReader(); // print the CustomerID of each record while (rdr.Read()) { Console.WriteLine(rdr[0]); } } finally { // close the reader if (rdr != null) { rdr.Close(); } // 5. Close the connection if (conn != null) { conn.Close(); } } } private void button3_Click(object sender, RoutedEventArgs e) { dbCC(); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)