Danh mục

Creating a Command Object Using Visual Studio .NET

Số trang: 3      Loại file: pdf      Dung lượng: 34.68 KB      Lượt xem: 9      Lượt tải: 0    
Thư viện của tui

Phí lưu trữ: miễn phí Tải xuống file đầy đủ (3 trang) 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 Command Object Using Visual Studio .NET To create a SqlCommand object using Visual Studio .NET (VS .NET), you drag a SqlCommand object from the Data tab of the Toolbox to your form.
Nội dung trích xuất từ tài liệu:
Creating a Command Object Using Visual Studio .NETCreating a Command Object Using Visual Studio .NETTo create a SqlCommand object using Visual Studio .NET (VS .NET), you drag aSqlCommand object from the Data tab of the Toolbox to your form. You can also drag anOleDbCommand object from the Data tab of the Toolbox to your form.Before you perform the procedure explained in this section, do the following: 1. Create a new project named MyDataReader containing a Windows application. 2. Add a SqlConnection object to your project (refer back to the previous chapter to see how to add a SqlConnection object using VS .NET). This object will have the default name of sqlConnection1. 3. Configure your sqlConnection1 object to access your Northwind database.Drag a SqlCommand object to your form. Figure 8.1 shows a form with a SqlCommandobject. This object is assigned the default name of sqlCommand1.Figure 8.1: A SqlCommand object in a formYou then set the Connection property for your sqlCommand1 using the drop-down list tothe right of the Connection property in the Properties window. You can select an existingConnection object from the drop-down list; you can also create a new Connection objectby selecting New from the list. For this example, select your existing sqlConnection1object for the Connection property of your sqlCommand1 object, as shown in Figure 8.1.You can use Query Builder to create a SQL statement by clicking on the ellipsis button tothe right of the CommandText property, and you can set parameters for a command byclicking the ellipsis button to the right of the Parameters property. Youll set theCommandText property of your SqlCommand object to a SELECT statement thatretrieves the CustomerID, CompanyName, and ContactName columns from theCustomers table. Youll construct this SELECT statement using Query Builder. To getstarted, click the ellipsis button to the right of the CommandText property for yourSqlCommand object.In the Add Table dialog, select the Customers table, as shown in Figure 8.2. Click theAdd button to add the Customers table to your query. Click the Close button to continue.Figure 8.2: Adding the Customers table to the query using the Add Table dialogNext, you construct your query using Query Builder. You select the columns you want toretrieve here. Add the CustomerID, CompanyName, and ContactName columns usingQuery Builder, as shown in Figure 8.3.Figure 8.3: Adding the CustomerID, CompanyName, and ContactName columns to thequery using Query BuilderClick the OK button to continue. The CommandText property of your SqlCommandobject is then set to the SELECT statement you created in Query Builder.Note Save your MyDataReader project by selecting File ➣ Save All. Youll see the use of the SqlCommand object you added to your project in the next chapter.

Tài liệu được xem nhiều: