Using ADO.NET Databases
Số trang: 11
Loại file: pdf
Dung lượng: 28.80 KB
Lượt xem: 18
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:
Sử dụng ADO.NET Cơ sở dữ liệu Với sự ra đời của NET Framework., Microsoft đã quyết định cập nhật mô hình của nó để truy cập cơ sở dữ liệu, ActiveX Data Objects (ADO), và tạo ra ADO.NET
Nội dung trích xuất từ tài liệu:
Using ADO.NET Databases Using ADO.NET DatabasesWith the advent of the .NET Framework, Microsoft decided to update its model foraccessing databases, ActiveX Data Objects (ADO), and created ADO.NET. ADO.NETcontains several enhancements over the original ADO architecture, providing improvedinteroperability and performance. If you are already familiar with ADO, you will noticethat the object model of ADO.NET is a little different. For one thing, the RecordSet typeno longer exists—Microsoft has created the TableAdapter and DataSet classes thatsupport disconnected data access and operations, allowing greater scalability because youno longer have to be connected to the database all the time. (To be fair, ADO provideddisconnected RecordSets, but they were the exception rather than the rule when used byprogrammers.) Therefore, your applications can consume fewer resources. With theconnection pooling mechanisms of ADO.NET, database connections can be reused bydifferent applications, thereby reducing the need to continually connect to and disconnectfrom the database, which can be a time-consuming operation.ADO.NET is designed to be easy to use, and Visual Studio 2005 provides several wizardsand other features that you can use to generate data access code.The Northwind Traders DatabaseNorthwind Traders is a fictitious company that sells edible goods with exotic names. TheNorthwind database contains several tables with information about the goods thatNorthwind Traders sells, the customers they sell to, orders placed by customers, suppliersthat Northwind Traders obtains goods from to re-sell, shippers that they can use to sendgoods to customers, and employees who work for Northwind Traders. Figure 23-1 showsall the tables in the Northwind Traders database and how they are related to each other.The tables that you will be using in this chapter are Orders and Products.Figure 23-1 The tables in the Northwind Traders database.Creating the DatabaseBefore proceeding further, you need to create the Northwind Traders database.Create the database 1. On the Windows Start menu, click All Programs, click Accessories, and then click Command Prompt to open a command prompt window. In the command prompt window, go to the Microsoft PressVisual CSharp Step by StepChapter 23 folder in your My Documents folder. 2. In the command prompt window, type the following command: sqlcmd –S YourServerSQLExpress –E –iinstnwnd.sql Replace YourServer with the name of your computer. TIP You can find the name of your computer by running the hostname command in the command prompt window, before running the sqlcmd command. This command uses the sqlcmd utility to connect to your local instance of SQL Server 2005 Express and run the instnwnd.sql script. This script contains the SQL commands that create the Northwind Traders database and the tables in the database, and fills them with some sample data. TIP Ensure SQL Server 2005 Express is running before you attempt to create the Northwind Traders database. You can check the status of SQL Server 2005 Express, and start it running if necessary, by using the SQL Configuration Manager available in the Configuration Tools folder of the Microsoft SQL Server 2005 CTP program group. 3. When the script finishes running, close the command prompt window.Accessing the DatabaseIn this set of exercises, you will write a program that connects to the database, retrievesthe contents of the Products and Suppliers tables, and displays their contents. In theNorthwind Traders database, each product is supplied by a single supplier, but anindividual supplier can supply more than one product.In the first exercise, you will create a data source that connects to the Northwind Tradersdatabase and retrieves the contents of these tables, by using the wizards provided withVisual Studio 2005.Create a data source 1. Using Visual Studio 2005, create a new project by using the Windows Application template. Name the project DisplayProducts and save it in the Microsoft PressVisual CSharp Step by StepChapter 23 folder in your My Documents folder. TIP If you cannot remember how to create a new Windows Application, refer to the first exercise, “Create the Middleshire Bell Ringers Association project,” in Chapter 20, “Introducing Windows Forms.”2. On the Data menu, click Add New Data Source. The Data Source Configuration Wizard starts. You use this wizard to create a connection to a data source. A data source can be a database, an object, or a Web service. Using an object or a Web service as a data source is beyond the scope of this book, but we will describe how to create and use Web services in Chapter 28, “Creating and Using a Web Service.”3. Ensur ...
Nội dung trích xuất từ tài liệu:
Using ADO.NET Databases Using ADO.NET DatabasesWith the advent of the .NET Framework, Microsoft decided to update its model foraccessing databases, ActiveX Data Objects (ADO), and created ADO.NET. ADO.NETcontains several enhancements over the original ADO architecture, providing improvedinteroperability and performance. If you are already familiar with ADO, you will noticethat the object model of ADO.NET is a little different. For one thing, the RecordSet typeno longer exists—Microsoft has created the TableAdapter and DataSet classes thatsupport disconnected data access and operations, allowing greater scalability because youno longer have to be connected to the database all the time. (To be fair, ADO provideddisconnected RecordSets, but they were the exception rather than the rule when used byprogrammers.) Therefore, your applications can consume fewer resources. With theconnection pooling mechanisms of ADO.NET, database connections can be reused bydifferent applications, thereby reducing the need to continually connect to and disconnectfrom the database, which can be a time-consuming operation.ADO.NET is designed to be easy to use, and Visual Studio 2005 provides several wizardsand other features that you can use to generate data access code.The Northwind Traders DatabaseNorthwind Traders is a fictitious company that sells edible goods with exotic names. TheNorthwind database contains several tables with information about the goods thatNorthwind Traders sells, the customers they sell to, orders placed by customers, suppliersthat Northwind Traders obtains goods from to re-sell, shippers that they can use to sendgoods to customers, and employees who work for Northwind Traders. Figure 23-1 showsall the tables in the Northwind Traders database and how they are related to each other.The tables that you will be using in this chapter are Orders and Products.Figure 23-1 The tables in the Northwind Traders database.Creating the DatabaseBefore proceeding further, you need to create the Northwind Traders database.Create the database 1. On the Windows Start menu, click All Programs, click Accessories, and then click Command Prompt to open a command prompt window. In the command prompt window, go to the Microsoft PressVisual CSharp Step by StepChapter 23 folder in your My Documents folder. 2. In the command prompt window, type the following command: sqlcmd –S YourServerSQLExpress –E –iinstnwnd.sql Replace YourServer with the name of your computer. TIP You can find the name of your computer by running the hostname command in the command prompt window, before running the sqlcmd command. This command uses the sqlcmd utility to connect to your local instance of SQL Server 2005 Express and run the instnwnd.sql script. This script contains the SQL commands that create the Northwind Traders database and the tables in the database, and fills them with some sample data. TIP Ensure SQL Server 2005 Express is running before you attempt to create the Northwind Traders database. You can check the status of SQL Server 2005 Express, and start it running if necessary, by using the SQL Configuration Manager available in the Configuration Tools folder of the Microsoft SQL Server 2005 CTP program group. 3. When the script finishes running, close the command prompt window.Accessing the DatabaseIn this set of exercises, you will write a program that connects to the database, retrievesthe contents of the Products and Suppliers tables, and displays their contents. In theNorthwind Traders database, each product is supplied by a single supplier, but anindividual supplier can supply more than one product.In the first exercise, you will create a data source that connects to the Northwind Tradersdatabase and retrieves the contents of these tables, by using the wizards provided withVisual Studio 2005.Create a data source 1. Using Visual Studio 2005, create a new project by using the Windows Application template. Name the project DisplayProducts and save it in the Microsoft PressVisual CSharp Step by StepChapter 23 folder in your My Documents folder. TIP If you cannot remember how to create a new Windows Application, refer to the first exercise, “Create the Middleshire Bell Ringers Association project,” in Chapter 20, “Introducing Windows Forms.”2. On the Data menu, click Add New Data Source. The Data Source Configuration Wizard starts. You use this wizard to create a connection to a data source. A data source can be a database, an object, or a Web service. Using an object or a Web service as a data source is beyond the scope of this book, but we will describe how to create and use Web services in Chapter 28, “Creating and Using a Web Service.”3. Ensur ...
Tìm kiếm theo từ khóa liên quan:
ngôn ngữ lập trình lập trình ngôn ngữ C# C# Summarizing Keyword Combinations Using ADO.NET DatabasesGợi ý tài liệu liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 274 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 265 0 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 265 0 0 -
Giáo trình Lập trình cơ bản với C++: Phần 1
77 trang 232 0 0 -
Bài giảng Một số hướng nghiên cứu và ứng dụng - Lê Thanh Hương
13 trang 224 0 0 -
Giáo án Tin học lớp 11 (Trọn bộ cả năm)
125 trang 217 1 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 206 0 0 -
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 184 0 0 -
Giáo trình Lập trình C căn bản: Phần 1
64 trang 169 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 166 0 0