1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Connecting to an ODBC Data Source ppt

3 496 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Connecting to an Odbc Data Source
Tác giả Team Lib
Trường học Unknown University
Chuyên ngành Computer Science
Thể loại Tài liệu
Năm xuất bản 2025
Thành phố Unknown
Định dạng
Số trang 3
Dung lượng 22,31 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

[ Team LiB ]Recipe 1.1 Connecting to an ODBC Data Source Problem You want to access your data source using an ODBC provider from your .NET application.. Solution Use the ODBC .NET dat

Trang 1

[ Team LiB ]

Recipe 1.1 Connecting to an ODBC Data Source

Problem

You want to access your data source using an ODBC provider from your NET

application

Solution

Use the ODBC NET data provider to access data exposed through an ODBC driver The sample code contains a single event handler:

Connect Button.Click

Creates an OdbcDataAdapter and uses it to fill a DataTable with the Category table from the Northwind sample database The default view of the table is bound

to a data grid on the form

The C# code is shown in Example 1-1

Example 1-1 File: OdbcConnectForm.cs

// Namespaces, variables, and constants

using System;

using System.Configuration;

using System.Data;

using System.Data.Odbc;

//

private void connectButton_Click(object sender, System.EventArgs e)

{

// Create the DataAdapter

String sqlSelect = "SELECT CategoryID, CategoryName, Description " +

"FROM Categories";

OdbcDataAdapter da = new OdbcDataAdapter(sqlSelect,

ConfigurationSettings.AppSettings["Odbc_ConnectString"]);

// Create the table, fill it, and bind the default view to the grid

DataTable dt = new DataTable( );

Trang 2

da.Fill(dt);

dataGrid.DataSource = dt.DefaultView;

}

Discussion

The ODBC NET data provider communicates with native ODBC drivers through COM interop (.NET's interoperability layer for COM) The following ODBC providers are guaranteed to be compatible with the ODBC.NET data provider:

• Microsoft SQL Server ODBC Driver

• Microsoft ODBC Driver for Oracle

• Microsoft Access (Jet) ODBC Driver

The NET data provider for ODBC connects to ODBC data sources through the

OdbcConnection object The ODBC driver connection string is specified using the

ConnectionString property It includes settings needed to establish the connection to the data source The connection string format closely matches the ODBC connection string format Additionally, you can specify an ODBC data source name (DSN) or file DSN by

setting the ConnectionString attribute to "DSN=myDSN" or "FileDSN=myFileDSN" For

more information about specifying ODBC connection strings, see the topic

"SQLDriverConnect" in the ODBC Programmer's Reference within MSDN Library Visual Studio also supports creating ODBC data source connections visually:

• Create a data connection in Server Explorer and drag it onto a form or design

surface Configure the OdbcConnection object that appears in the component tray

• Drag an OdbcConnection from the Data tab of the Toolbox onto a form or design

surface Configure the ConnectionString property in the Properties window of the OdbcConnection object that appears

The NET ODBC data provider requires a reference to the System.Data.Odbc namespace

in NET Framework Version 1.1 In Version 1.0, the namespace is Microsoft.Data.Odbc Add a NET Reference to Microsoft.Data.Odbc.dll for a NET Framework Version 1.0 project

The NET ODBC NET data provider ships with NET Framework Version 1.1 The data provider can be downloaded from http://msdn.microsoft.com/downloads for NET

Framework Version 1.0

[ Team LiB ]

Ngày đăng: 24/12/2013, 05:15

TỪ KHÓA LIÊN QUAN