Connecting to Access and Oracle Databases
Số trang: 6
Loại file: pdf
Dung lượng: 29.01 KB
Lượt xem: 17
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:
Connecting to Access and Oracle Databases In this section youll see examples of connecting to both an Access and an Oracle database. To interact with either of these databases in your program
Nội dung trích xuất từ tài liệu:
Connecting to Access and Oracle DatabasesConnecting to Access and Oracle DatabasesIn this section youll see examples of connecting to both an Access and an Oracledatabase. To interact with either of these databases in your program, you use classes fromthe System.Data.OleDb namespace. This namespace contains classes for use withdatabases that support object linking and embedding for databases (OLE DB) such asAccess or Oracle. Youll learn more about the System.Data.OleDb namespace in Chapter5, Overview of the ADO.NET Classes.Connecting to an Access DatabaseYou connect to an Access database using an OleDbConnection object-rather than aSqlConnection object-with a connection string of the following format:provider=Microsoft.Jet.OLEDB.4.0;data source=databaseFilewhere databaseFile is the directory and filename of your Access database. Notice thatyou specify the provider in the connection string, which is set toMicrosoft.Jet.OLEDB.4.0.The following example creates a string named connectionString with the appropriateformat to connect to the Access Northwind database stored in the Northwind.mdb file:string connectionString = provider=Microsoft.Jet.OLEDB.4.0; + data source=F:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb;Note Notice the use of two backslash characters in the data source part of the connection string. The first backslash is used to specify that the second backslash is to be treated literally; therefore \ is treated as in the connection string. Youll need to locate the Northwind.mdb file on your hard disk and set your connection string appropriately.Assuming the System.Data.OleDb namespace has been imported, the following examplecreates an OleDbConnection object, passing connectionString (set in the previous line ofcode) to the constructor:OleDbConnection myOleDbConnection = new 01eDbConnection(connectionString);Listing 1.2 illustrates how to connect to the Northwind Access database using anOleDbConnection object and retrieve a row from the Customers table. Notice that youuse an OleDbCommand and OleDbDataReader object to run a SQL statement and readthe returned results from an Access database.Listing 1.2: OLEDBCONNECTIONACCESS.CS/* OleDbConnectionAccess.cs illustrates how to use an OleDbConnection object to connect to an Access database*/using System;using System.Data;using System.Data.OleDb;class OleDbConnectionAccess{ public static void Main() { // formulate a string containing the details of the // database connection string connectionString = provider=Microsoft.Jet.OLEDB.4.0; + data source=F:\Program Files\MicrosoftOffice\Office\Samples\Northwind.mdb; // create an OleDbConnection object to connect to the // database, passing the connection string to the constructor OleDbConnection myOleDbConnection = new OleDbConnection(connectionString); // create an OleDbCommand object OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand(); // set the CommandText property of the OleDbCommand object to // a SQL SELECT statement that retrieves a row from the Customers table myOleDbCommand.CommandText = SELECT CustomerID, CompanyName, ContactName, Address + FROM Customers + WHERE CustomerID = ALFKI; // open the database connection using the // Open() method of the OleDbConnection object myOleDbConnection.Open(); // create an OleDbDataReader object and call the ExecuteReader() // method of the OleDbCommand object to run the SELECT statement OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader(); // read the row from the OleDbDataReader object using // the Read() method myOleDbDataReader.Read(); // display the column values Console.WriteLine(myOleDbDataReader[ CustomerID] = + myOleDbDataReader[CustomerID]); Console.WriteLine(myOleDbDataReader[ CompanyName] = + myOleDbDataReader[CompanyName]); Console.WriteLine(myOleDbDataReader[ ContactName] = + myOleDbDataReader[ContactName]); Console.WriteLine(myOleDbDataReader[ Address] = + myOleDbDataReader[Address]); // close the OleDbDataReader object using the Close() method myOleDbDataReader.Close(); // close the OleDbConnection object using the Close() method myOleDbConnection.Close(); }}The output from this program is as follows:myOleDbDataReader[CustomerID] = ALFKImyOleDbDataReader[CompanyName] = Alfreds FutterkistemyOleDbDataReader[ContactName] = Maria AndersmyOleDbDataReader[Address] = Obere Str. 57Connecting to an Oracle DatabaseYou connect to an Oracle database using an OleDbConnection object with a connectionstring of the following format:provider=MSDAORA;data source=OracleNetServiceName;userid=username;password=passwordwhere • OracleNetServ ...
Nội dung trích xuất từ tài liệu:
Connecting to Access and Oracle DatabasesConnecting to Access and Oracle DatabasesIn this section youll see examples of connecting to both an Access and an Oracledatabase. To interact with either of these databases in your program, you use classes fromthe System.Data.OleDb namespace. This namespace contains classes for use withdatabases that support object linking and embedding for databases (OLE DB) such asAccess or Oracle. Youll learn more about the System.Data.OleDb namespace in Chapter5, Overview of the ADO.NET Classes.Connecting to an Access DatabaseYou connect to an Access database using an OleDbConnection object-rather than aSqlConnection object-with a connection string of the following format:provider=Microsoft.Jet.OLEDB.4.0;data source=databaseFilewhere databaseFile is the directory and filename of your Access database. Notice thatyou specify the provider in the connection string, which is set toMicrosoft.Jet.OLEDB.4.0.The following example creates a string named connectionString with the appropriateformat to connect to the Access Northwind database stored in the Northwind.mdb file:string connectionString = provider=Microsoft.Jet.OLEDB.4.0; + data source=F:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb;Note Notice the use of two backslash characters in the data source part of the connection string. The first backslash is used to specify that the second backslash is to be treated literally; therefore \ is treated as in the connection string. Youll need to locate the Northwind.mdb file on your hard disk and set your connection string appropriately.Assuming the System.Data.OleDb namespace has been imported, the following examplecreates an OleDbConnection object, passing connectionString (set in the previous line ofcode) to the constructor:OleDbConnection myOleDbConnection = new 01eDbConnection(connectionString);Listing 1.2 illustrates how to connect to the Northwind Access database using anOleDbConnection object and retrieve a row from the Customers table. Notice that youuse an OleDbCommand and OleDbDataReader object to run a SQL statement and readthe returned results from an Access database.Listing 1.2: OLEDBCONNECTIONACCESS.CS/* OleDbConnectionAccess.cs illustrates how to use an OleDbConnection object to connect to an Access database*/using System;using System.Data;using System.Data.OleDb;class OleDbConnectionAccess{ public static void Main() { // formulate a string containing the details of the // database connection string connectionString = provider=Microsoft.Jet.OLEDB.4.0; + data source=F:\Program Files\MicrosoftOffice\Office\Samples\Northwind.mdb; // create an OleDbConnection object to connect to the // database, passing the connection string to the constructor OleDbConnection myOleDbConnection = new OleDbConnection(connectionString); // create an OleDbCommand object OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand(); // set the CommandText property of the OleDbCommand object to // a SQL SELECT statement that retrieves a row from the Customers table myOleDbCommand.CommandText = SELECT CustomerID, CompanyName, ContactName, Address + FROM Customers + WHERE CustomerID = ALFKI; // open the database connection using the // Open() method of the OleDbConnection object myOleDbConnection.Open(); // create an OleDbDataReader object and call the ExecuteReader() // method of the OleDbCommand object to run the SELECT statement OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader(); // read the row from the OleDbDataReader object using // the Read() method myOleDbDataReader.Read(); // display the column values Console.WriteLine(myOleDbDataReader[ CustomerID] = + myOleDbDataReader[CustomerID]); Console.WriteLine(myOleDbDataReader[ CompanyName] = + myOleDbDataReader[CompanyName]); Console.WriteLine(myOleDbDataReader[ ContactName] = + myOleDbDataReader[ContactName]); Console.WriteLine(myOleDbDataReader[ Address] = + myOleDbDataReader[Address]); // close the OleDbDataReader object using the Close() method myOleDbDataReader.Close(); // close the OleDbConnection object using the Close() method myOleDbConnection.Close(); }}The output from this program is as follows:myOleDbDataReader[CustomerID] = ALFKImyOleDbDataReader[CompanyName] = Alfreds FutterkistemyOleDbDataReader[ContactName] = Maria AndersmyOleDbDataReader[Address] = Obere Str. 57Connecting to an Oracle DatabaseYou connect to an Oracle database using an OleDbConnection object with a connectionstring of the following format:provider=MSDAORA;data source=OracleNetServiceName;userid=username;password=passwordwhere • OracleNetServ ...
Tìm kiếm theo từ khóa liên quan:
kĩ thuật lập trình công nghệ thông tin lập trình ngôn ngữ lập trình C Shark C# sybex - c.sharp database programming Connecting to Access and Oracle DatabasesGợi ý tài liệu liên quan:
-
52 trang 429 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 311 0 0 -
74 trang 294 0 0
-
96 trang 290 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 288 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 278 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 272 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 271 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 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 262 0 0