Connecting to an ODBC Data Source
Số trang: 3
Loại file: pdf
Dung lượng: 22.31 KB
Lượt xem: 11
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
[ 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.
Nội dung trích xuất từ tài liệu:
Connecting to an ODBC Data Source[ Team LiB ]Recipe 1.1 Connecting to an ODBC Data SourceProblemYou want to access your data source using an ODBC provider from your .NETapplication.SolutionUse 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 constantsusing 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( ); da.Fill(dt); dataGrid.DataSource = dt.DefaultView;}DiscussionThe ODBC .NET data provider communicates with native ODBC drivers through COMinterop (.NETs interoperability layer for COM). The following ODBC providers areguaranteed to be compatible with the ODBC.NET data provider: • Microsoft SQL Server ODBC Driver • Microsoft ODBC Driver for Oracle • Microsoft Access (Jet) ODBC DriverThe .NET data provider for ODBC connects to ODBC data sources through theOdbcConnection object. The ODBC driver connection string is specified using theConnectionString property. It includes settings needed to establish the connection to thedata source. The connection string format closely matches the ODBC connection stringformat. Additionally, you can specify an ODBC data source name (DSN) or file DSN bysetting the ConnectionString attribute to DSN=myDSN or FileDSN=myFileDSN. Formore information about specifying ODBC connection strings, see the topicSQLDriverConnect in the ODBC Programmers 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 namespacein .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.0project.The .NET ODBC .NET data provider ships with .NET Framework Version 1.1. The dataprovider can be downloaded from http://msdn.microsoft.com/downloads for .NETFramework Version 1.0.[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Connecting to an ODBC Data Source[ Team LiB ]Recipe 1.1 Connecting to an ODBC Data SourceProblemYou want to access your data source using an ODBC provider from your .NETapplication.SolutionUse 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 constantsusing 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( ); da.Fill(dt); dataGrid.DataSource = dt.DefaultView;}DiscussionThe ODBC .NET data provider communicates with native ODBC drivers through COMinterop (.NETs interoperability layer for COM). The following ODBC providers areguaranteed to be compatible with the ODBC.NET data provider: • Microsoft SQL Server ODBC Driver • Microsoft ODBC Driver for Oracle • Microsoft Access (Jet) ODBC DriverThe .NET data provider for ODBC connects to ODBC data sources through theOdbcConnection object. The ODBC driver connection string is specified using theConnectionString property. It includes settings needed to establish the connection to thedata source. The connection string format closely matches the ODBC connection stringformat. Additionally, you can specify an ODBC data source name (DSN) or file DSN bysetting the ConnectionString attribute to DSN=myDSN or FileDSN=myFileDSN. Formore information about specifying ODBC connection strings, see the topicSQLDriverConnect in the ODBC Programmers 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 namespacein .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.0project.The .NET ODBC .NET data provider ships with .NET Framework Version 1.1. The dataprovider can be downloaded from http://msdn.microsoft.com/downloads for .NETFramework Version 1.0.[ Team LiB ]
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Connecting to an ODBC Data SourceTài liệu liên quan:
-
52 trang 434 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 320 0 0 -
74 trang 303 0 0
-
96 trang 299 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 291 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 286 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 270 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 269 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0