Danh mục

Using a DataReader Object in Visual Studio .NET

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

Phí lưu trữ: miễn phí Tải xuống file đầy đủ (4 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:

Using a DataReader Object in Visual Studio .NET You cant visually create a DataReader object in Visual Studio .NET (VS .NET); you can only create them using program statements.
Nội dung trích xuất từ tài liệu:
Using a DataReader Object in Visual Studio .NETUsing a DataReader Object in Visual Studio .NETYou cant visually create a DataReader object in Visual Studio .NET (VS .NET); you canonly create them using program statements.In this section, youll see how to create a SqlDataReader object and use it to retrieve theresult set from a SqlCommand object, which you saw how to create using VS .NET in theprevious chapter. That SqlCommand object contained a SELECT statement that retrievedthe CustomerID, CompanyName, and ContactName columns from the Customers table.Youll see how to execute this SELECT statement, read the result set using theSqlDataReader object, and display the result set in a ListView control. A ListViewcontrol allows you to view information laid out in a grid.Note You can either modify the MyDataReader project you created in the previous chapter, or if you dont want to follow along with the instructions in this section, you can simply open the completed VS .NET project contained in the DataReader directory. To open the completed project, select File ➣ Open ➣ Project, browse to the VS .NET projectsDataReader directory, and open the WindowsApplication4.csproj file.If you are modifying your existing Windows application, drag a ListView control to yourform. Figure 9.2 shows a form with a ListView control. Make sure the Name property ofyour ListView is set to listView1 (this is the default name, so you shouldnt have tochange it).Figure 9.2: Adding a ListView control to the formWarning If you opened the completed project, you dont have to add a ListView controI; its already on the completed form. You will need to change the ConnectionString property of the sqlConnection1 object so that it connects to your SQL Server Northwind database. Once youve set your ConnectionString, you can run the form by selecting Debug ➣ Start Without Debugging.Next, double-click an area on your form outside the ListView control. This causes VS.NET to display the code editor, and youll see the cursor positioned in the Form1_Load()method; this method is called when your form is initially loaded at runtime. Typically,this is the method by which you want to execute your database operations. Set yourForm1_Load() method to the following code:private void Form1_Load(object sender, System.EventArgs e){ sqlConnection1.Open(); System.Data.SqlClient.SqlDataReader mySqlDataReader = sqlCommand1.ExecuteReader(); while (mySqlDataReader.Read()) { listView1.Items.Add(mySqlDataReader[CustomerID].ToString()); listView1.Items.Add(mySqlDataReader[CompanyName].ToString()); listView1.Items.Add(mySqlDataReader[ContactName].ToString()); } mySqlDataReader.Close(); sqlConnection1.Close();}Notice you add an item to the ListView control using the Add() method, which isaccessed using the Items property. The Add() method expects a string parameter, and youtherefore call the ToString() method to convert the object returned by the SqlDataReaderobject to a string. Also notice you include the namespace when referencing theSqlDataReader class: you use System.Data.SqlClient .SqlDataReader when creating theSqlDataReader object.The previous code opens the database connection, creates a SqlDataReader object, anduses it to read the rows from the result set returned by the SqlCommand object. Eachcolumn of the result set is then added to the ListView control using the Add() method.Figure 9.3 shows the completed Form1_Load() method.Figure 9.3: The completed Form1_Load() methodBefore you run your form, youll need to add a substring containing the password for thedatabase connection to the ConnectString property of your SqlConnection object. For myinstallation of SQL Server, the password to access the Northwind database is sa, and myConnectionString property is set to:data source=localhost;initial catalog=Northwind;persist security info=False;userid=sa;pwd=sa;workstation id=JMPRICE-DT1;packet size=4096Notice the substring pwd=sa in this string to set the password.Finally, run your form by pressing Ctl+F5 on your keyboard, or select Debug â StartWithout Debugging. Figure 9.4 shows the running form.Figure 9.4: The running formSave your MyDataReader project by selecting File ➣ Save All. Youll use this project inlater chapters. If you used the completed DataReader project rather than modifying yourexisting project, dont worry: youll be able to use the completed DataReader project inthe later chapters also.

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