Технология Microsoft ADO.NET


Объект Command - часть 2


Закрываем соединение явным образом:

conn.Close();

Все! Запускаем приложение. Мы получили уже знакомый результат, но теперь мы действительно управляем всеми объектами, работающими с данными.

Полный листинг проекта ConnectionMDB:

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb;

namespace ConnectionMDB { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid dataGrid1; string ConnectionString = @"Provider=""Microsoft.Jet.OLEDB.4.0""; Data Source=""E:\Program Files\Microsoft Visual Studio .NET 2003\Crystal Reports\Samples\Database\xtreme.mdb" ";User ID=Admin;Jet OLEDB:Encrypt Database=False";

string commandText = @"SELECT Address1, Address2, City, [Contact First Name], [Contact Last Name], [Contact Position], [Contact Title], Country, [Customer Credit ID], [Customer ID], [Customer Name], [E-mail], Fax, [Last Year's Sales], Phone, [Postal Code], Region, [Web Site] FROM Customer";

/// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null;

public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); OleDbConnection conn = new OleDbConnection(ConnectionString); //OleDbConnection conn = new OleDbConnection(); //conn.ConnectionString = ConnectionString; conn.Open(); OleDbCommand myCommand = new OleDbCommand(); myCommand.Connection = conn; myCommand.CommandText = commandText; //OleDbCommand myCommand = conn.CreateCommand(); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(); dataAdapter.SelectCommand = myCommand; DataSet ds = new DataSet(); dataAdapter.Fill(ds, "Customers"); dataGrid1.DataSource = ds.Table ["Customers"].DefaultView; conn.Close();

// // TODO: Add any constructor code after InitializeComponent call // }




Начало  Назад  Вперед



Книжный магазин