Creating a DataAdapter Object Using Visual Studio .NET
Số trang: 4
Loại file: pdf
Dung lượng: 45.07 KB
Lượt xem: 10
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:
Creating a DataAdapter Object Using Visual Studio .NET In this section, youll learn how to create a DataAdapter using Visual Studio .NET. Note Youll find a completed VS
Nội dung trích xuất từ tài liệu:
Creating a DataAdapter Object Using Visual Studio .NET Creating a DataAdapter Object Using Visual Studio .NETIn this section, youll learn how to create a DataAdapter using Visual Studio .NET.Note Youll find a completed VS .NET project for this section in the DataAdapter directory. You can open this project in VS .NET by selecting File ➣ Open ➣ Project and opening the WindowsApplication4.csproj file. Youll need to change the ConnectionString property of the sqlConnection1 object to connect to your database. You can also follow along with the instructions in this section by copying the DataReader directory to another directory and using that project as your starting point.Open your form by double-clicking Form1.cs in the Solution Explorer window. Next,create a SqlDataAdapter object by dragging a SqlDataAdapter object from the Data tab ofthe Toolbox to your form. When you drag a SqlDataAdapter object to your form, youstart the Data Adapter Configuration Wizard, as shown in Figure 10.8.Figure 10.8: The Data Adapter Configuration WizardClick the Next button to continue.You now select the database connection you want to use, or you can create a new one.Pick your connection to the Northwind database (or create a new connection if you donthave an existing one), as shown in Figure 10.9.Figure 10.9: Choosing your data connectionClick the Next button to continue.Next, you set your query type to Use SQL statements. As you can see from Figure10.10, you set your query type to use SQL statements, create new stored procedures, oruse existing stored procedures. The SQL statements or stored procedures are then used inthe SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand propertiesof your SqlDataAdapter object. Youll learn about the latter three properties in Chapter11; they are used to insert, update, and delete rows.Figure 10.10: Choosing your query typeMake sure youve picked Use SQL statements, and click the Next button to continue.You now generate the SQL statements for your SqlDataAdapter. You can either enter aSELECT statement directly by typing it or you can press the Query Builder button tobuild your SELECT statement visually. Enter the SELECT statement, as shown in Figure10.11, and click the Next button to continue.Figure 10.11: Generating the SQL statementsThe SELECT statement you entered is now used to generate the INSERT, UPDATE, andDELETE statements along with the table mappings. Figure 10.12 shows the final dialogbox for the Data Adapter Configuration Wizard.Figure 10.12: Final dialog box for the Data Adapter Configuration WizardClick the Finish button to complete the Wizard. A SqlDataAdapter object namedsqlDataAdapter1 is now added to the tray beneath your form, as shown in Figure 10.13.Figure 10.13: The new SqlDataAdapter object in the trayWarning You need to set the Connection property of the SelectCommand in your sqlDataAdapter1 object to your Connection object before the DataAdapter can access the database. You do this using the Properties window by drilling down from SelectCommand to Connection. You then click the drop-down list, select Existing, and select your SqlConnection object, which should be named sqlConnection1. Also check the ConnectionString property of your SqlConnection object to make sure it is set to connect to your Northwind database. If you dont do this step, youll get an error stating that your SqlDataAdapter object hasnt been configured properly.Notice the three links at the bottom of the Properties window for sqlDataAdapter1: • Configure Data Adapter This link allows you to re-enter the Wizard to configure your DataAdapter. • Generate Dataset This link allows you to generate a DataSet object using the information set for your DataAdapter. Youll use this link in the next section to generate a new DataSet. • Preview Data This link allows you to preview the data returned by the SelectCommand of your DataAdapter.Feel free to examine the code generated by the Wizard in your form for thesqlDataAdapter1 object. When youre ready, select File ➣ Save All.Note Dont bother running your project yet because youll add a DataSet that will be populated using your DataAdapter in the next section.
Nội dung trích xuất từ tài liệu:
Creating a DataAdapter Object Using Visual Studio .NET Creating a DataAdapter Object Using Visual Studio .NETIn this section, youll learn how to create a DataAdapter using Visual Studio .NET.Note Youll find a completed VS .NET project for this section in the DataAdapter directory. You can open this project in VS .NET by selecting File ➣ Open ➣ Project and opening the WindowsApplication4.csproj file. Youll need to change the ConnectionString property of the sqlConnection1 object to connect to your database. You can also follow along with the instructions in this section by copying the DataReader directory to another directory and using that project as your starting point.Open your form by double-clicking Form1.cs in the Solution Explorer window. Next,create a SqlDataAdapter object by dragging a SqlDataAdapter object from the Data tab ofthe Toolbox to your form. When you drag a SqlDataAdapter object to your form, youstart the Data Adapter Configuration Wizard, as shown in Figure 10.8.Figure 10.8: The Data Adapter Configuration WizardClick the Next button to continue.You now select the database connection you want to use, or you can create a new one.Pick your connection to the Northwind database (or create a new connection if you donthave an existing one), as shown in Figure 10.9.Figure 10.9: Choosing your data connectionClick the Next button to continue.Next, you set your query type to Use SQL statements. As you can see from Figure10.10, you set your query type to use SQL statements, create new stored procedures, oruse existing stored procedures. The SQL statements or stored procedures are then used inthe SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand propertiesof your SqlDataAdapter object. Youll learn about the latter three properties in Chapter11; they are used to insert, update, and delete rows.Figure 10.10: Choosing your query typeMake sure youve picked Use SQL statements, and click the Next button to continue.You now generate the SQL statements for your SqlDataAdapter. You can either enter aSELECT statement directly by typing it or you can press the Query Builder button tobuild your SELECT statement visually. Enter the SELECT statement, as shown in Figure10.11, and click the Next button to continue.Figure 10.11: Generating the SQL statementsThe SELECT statement you entered is now used to generate the INSERT, UPDATE, andDELETE statements along with the table mappings. Figure 10.12 shows the final dialogbox for the Data Adapter Configuration Wizard.Figure 10.12: Final dialog box for the Data Adapter Configuration WizardClick the Finish button to complete the Wizard. A SqlDataAdapter object namedsqlDataAdapter1 is now added to the tray beneath your form, as shown in Figure 10.13.Figure 10.13: The new SqlDataAdapter object in the trayWarning You need to set the Connection property of the SelectCommand in your sqlDataAdapter1 object to your Connection object before the DataAdapter can access the database. You do this using the Properties window by drilling down from SelectCommand to Connection. You then click the drop-down list, select Existing, and select your SqlConnection object, which should be named sqlConnection1. Also check the ConnectionString property of your SqlConnection object to make sure it is set to connect to your Northwind database. If you dont do this step, youll get an error stating that your SqlDataAdapter object hasnt been configured properly.Notice the three links at the bottom of the Properties window for sqlDataAdapter1: • Configure Data Adapter This link allows you to re-enter the Wizard to configure your DataAdapter. • Generate Dataset This link allows you to generate a DataSet object using the information set for your DataAdapter. Youll use this link in the next section to generate a new DataSet. • Preview Data This link allows you to preview the data returned by the SelectCommand of your DataAdapter.Feel free to examine the code generated by the Wizard in your form for thesqlDataAdapter1 object. When youre ready, select File ➣ Save All.Note Dont bother running your project yet because youll add a DataSet that will be populated using your DataAdapter in the next section.
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 Creating a DataAdapter Object Using Visual Studio .NETGợ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