1. Trang chủ
  2. » Công Nghệ Thông Tin

Beginning C# 2008 Databases From Novice to Professional phần 7 pot

52 326 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 52
Dung lượng 724,05 KB

Nội dung

// create data view DataView dv = new DataView( dt, "country = 'Germany'", "country", DataViewRowState.CurrentRows ); // display data from data view foreach (DataRowView drv in dv) { for (int i = 0; i < dv.Table.Columns.Count; i++) Console.Write(drv[i] + "\t"); Console.WriteLine(); } } catch(Exception e) { Console.WriteLine("Error: " + e); } finally { // close connection conn.Close(); } } } } 3. Make DataViews the startup project, and run it by pressing Ctrl+F5. You should see the results in Figure 13-5. Figure 13-5. Using a data view CHAPTER 13 ■ USING DATASETS AND DATA ADAPTERS 283 9004ch13final.qxd 12/13/07 4:05 PM Page 283 How It Works This program is basically the same as the other examples, so we’ll focus on its use of a data view. You create a new data view and initialize it by passing four parameters to its constructor. // create data view DataView dv = new DataView( dt, "country = 'Germany'", "country", DataViewRowState.CurrentRows ); The first parameter is a data table, the second is a filter for the contents of the data table, the third is the sort column, and the fourth specifies the types of rows to include in the data view. System.Data.DataViewRowState is an enumeration of states that rows can have in a data view’s underlying data table. Table 13-1 summarizes the states. Table 13-1. Data View Row States DataViewRowState Members Description Added A new row CurrentRows Current rows including unchanged, new, and modified ones Deleted A deleted row ModifiedCurrent The current version of a modified row ModifiedOriginal The original version of a modified row None None of the rows OriginalRows Original rows, including unchanged and deleted rows Unchanged A r ow that hasn’t been modified Every time a row is added, modified, or deleted, its row state changes to the appro- priate one in Table 13-1. This is useful if you’re interested in retrieving, sorting, or filtering specific rows based on their state (for example, all new rows in the data table or all rows that have been modified). You then loop through the rows in the data view. CHAPTER 13 ■ USING DATASETS AND DATA ADAPTERS284 9004ch13final.qxd 12/13/07 4:05 PM Page 284 // display data from data view foreach (DataRowView drv in dv) { for (int i = 0; i < dv.Table.Columns.Count; i++) Console.Write(drv[i] + "\t"); Console.WriteLine(); } Just as a data row represents a single row in a data table, a data row view (perhaps it would have been better to call it a data view row) represents a single row in a data view. You retrieve the filtered and the sorted column data for each data row view and output it to the console. As this simple example suggests, data views offer a powerful and flexible means of dynamically changing what data one works within a data table. Modifying Data in a Dataset In the following sections, you’ll work through a practical example showing a number of ways to update data in data tables programmatically. Note that here you’ll just modify the data in the dataset but not update the data in the database.You’ll see in the “Propagating Changes to a Data Source” section how to persist the original data source changes made to a dataset. ■Note Changes you make to a dataset aren’t automatically propagated to a database. To save the changes in a database, you need to connect to the database again and explicitly perform the necessary updates. Try It Out: Modifying a Data Table in a Dataset Let’s update a row and add a row in a data table. 1. Add a new C# Console Application project named ModifyDataTable to your Chapter13 solution. Rename Program.cs to ModifyDataTable.cs. 2. Replace the code in ModifyDataTable.cs with the code in Listing 13-4. CHAPTER 13 ■ USING DATASETS AND DATA ADAPTERS 285 9004ch13final.qxd 12/13/07 4:05 PM Page 285 Listing 13-4. ModifyDataTable.cs u sing System; u sing System.Data; u sing System.Data.SqlClient; namespace Chapter13 { class ModifyDataTable { static void Main(string[] args) { // connection string string connString = @" server = .\sqlexpress; integrated security = true; database = northwind "; // query string sql = @" select * from employees where country = 'UK' "; // create connection SqlConnection conn = new SqlConnection(connString); try { // create data adapter SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(sql, conn); // create and fill dataset DataSet ds = new DataSet(); da.Fill(ds, "employees"); // get data table reference DataTable dt = ds.Tables["employees"]; CHAPTER 13 ■ USING DATASETS AND DATA ADAPTERS286 9004ch13final.qxd 12/13/07 4:05 PM Page 286 // FirstName column should be nullable dt.Columns["firstname"].AllowDBNull = true; // modify city in first row dt.Rows[0]["city"] = "Wilmington"; // add a row DataRow newRow = dt.NewRow(); newRow["firstname"] = "Roy"; newRow["lastname"] = "Beatty"; newRow["titleofcourtesy"] = "Sir"; newRow["city"] = "Birmingham"; newRow["country"] = "UK"; dt.Rows.Add(newRow); // display rows foreach (DataRow row in dt.Rows) { Console.WriteLine( "{0} {1} {2}", row["firstname"].ToString().PadRight(15), row["lastname"].ToString().PadLeft(25), row["city"]); } // // code for updating the database would come here // } catch(Exception e) { Console.WriteLine("Error: " + e); } finally { // close connection conn.Close(); } } } } CHAPTER 13 ■ USING DATASETS AND DATA ADAPTERS 287 9004ch13final.qxd 12/13/07 4:05 PM Page 287 3. Make ModifyDataTable the startup project, and run it by pressing Ctrl+F5. You should see the results in Figure 13-6. Figure 13-6. Modifying a data table How It Works As before, you use a single data table in a dataset. // get data table reference DataTable dt = ds.Tables["employees"]; Next, you can see an example of how you can change the schema infor mation. You select the FirstName column, whose AllowNull property is set to false in the database, and you change it—just for the purposes of demonstration—to true. // FirstName column should be nullable dt.Columns["firstname"].AllowDBNull = true; Note that you can use an ordinal index (for example, dt.Columns[1]) if you know what the index for the column is, but using * to select all columns makes this less reliable since the position of a column may change if the database table schema changes. You can modify a row using the same technique. You simply select the appropriate row and set its columns to whatever values you want, consistent with the column data types, of course. The following line shows the City column of the first row of the dataset being changed to Wilmington. // modify City in first row dt.Rows[0]["city"] = "Wilmington"; Next you add a new row to the data table. CHAPTER 13 ■ USING DATASETS AND DATA ADAPTERS288 9004ch13final.qxd 12/13/07 4:05 PM Page 288 // add a row DataRow newRow = dt.NewRow(); newRow["firstname"] = "Roy"; newRow["lastname"] = "Beatty"; newRow["titleofcourtesy"] = "Sir"; newRow["city"] = "Birmingham"; newRow["country"] = "UK"; dt.Rows.Add(newRow); The NewRow method creates a data row (a System.Data.DataRow instance). You use the data row’s indexer to assign values to its columns. Finally, you add the new row to the data table, calling the Add method on the data table’s Rows property, which references the rows collection. Note that you don’t provide a value for EmployeeID since it’s an IDENTITY column. If you were to persist the changes to the database, SQL Server would automatically provide a value for it. Updating data sources requires learning more about data adapter methods and properties. Let’s take a look at these now. Propagating Changes to a Data Source You’ve seen how a data adapter populates a dataset’s data tables. What you haven’t looked at yet is how a data adapter updates and synchronizes a data source with data from a dataset. It has three properties that support this (analogous to its SelectCommand property, which supports queries). • UpdateCommand • InsertCommand • DeleteCommand W e’ll describe each of these properties briefly and then put them to work. UpdateCommand Property The UpdateCommand pr operty The UpdateCommand pr operty of the data adapter holds the command used to update the data source when the data adapter’s Update method is called. CHAPTER 13 ■ USING DATASETS AND DATA ADAPTERS 289 9004ch13final.qxd 12/13/07 4:05 PM Page 289 For example, to update the City column in the Employees table with the data from a data table, one approach is to write code such as the following (where da is the data adapter, dt is the data table, conn is the connection, and ds is the dataset): // create command to update Employees City column da.UpdateCommand = new SqlCommand( "update employees " + "set " + " city = " + "'" + dt.Rows[0]["city"] + "' " + "where employeeid = " + "'" + dt.Rows[0]["employeeid"] + "' " , conn); // update Employees table da.Update(ds, "employees"); This isn’t very pretty—or useful. Basically, you code an UPDATE statement and embed two data column values for the first row in a data table in it. It’s valid SQL, but that’s its only virtue and it’s not much of one, since it updates only one database row, the row in Employees corresponding to the first data row in the employees data table. Another approach works for any number of rows. Recall from the Command Parameters program in Chapter 11 how you used command parameters for INSERT statements. You can use them in any query or data manipulation statement. Let’s recode the preceding code with command parameters. Try It Out: Propagating Dataset Changes to a Data Source Here you’ll change the city in the first row of the Employees table and persist the change in the database. 1. Add a new C# Console Application project named PersistChanges to your Chapter13 solution. Rename Program.cs to PersistChanges.cs. 2. Replace the code in PersistChanges.cs with the code in Listing 13-5. (This is a variation on ModifyDataTable.cs in Listing 13-4, with the nullability and inser- tion logic removed since they’re irrelevant here.) CHAPTER 13 ■ USING DATASETS AND DATA ADAPTERS290 9004ch13final.qxd 12/13/07 4:05 PM Page 290 Listing 13-5. PersistChanges.cs u sing System; u sing System.Data; u sing System.Data.SqlClient; namespace Chapter13 { class PersistChanges { static void Main(string[] args) { // connection string string connString = @" server = .\sqlexpress; integrated security = true; database = northwind "; // query string qry = @" select * from employees where country = 'UK' "; // SQL to update employees string upd = @" update employees set city = @city where employeeid = @employeeid "; // create connection SqlConnection conn = new SqlConnection(connString); CHAPTER 13 ■ USING DATASETS AND DATA ADAPTERS 291 9004ch13final.qxd 12/13/07 4:05 PM Page 291 try { // create data adapter SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(qry, conn); // create and fill dataset DataSet ds = new DataSet(); da.Fill(ds, "employees"); // get data table reference DataTable dt = ds.Tables["employees"]; // modify city in first row dt.Rows[0]["city"] = "Wilmington"; // display rows foreach (DataRow row in dt.Rows) { Console.WriteLine( "{0} {1} {2}", row["firstname"].ToString().PadRight(15), row["lastname"].ToString().PadLeft(25), row["city"]); } // update Employees // // create command SqlCommand cmd = new SqlCommand(upd, conn); // // map parameters // // City cmd.Parameters.Add( "@city", SqlDbType.NVarChar, 15, "city"); CHAPTER 13 ■ USING DATASETS AND DATA ADAPTERS292 9004ch13final.qxd 12/13/07 4:05 PM Page 292 [...]... pressing Ctrl+F5 You should see the result in Figure 13 -7 Figure 13 -7 Modifying a row 293 9004ch13final.qxd 294 12/13/ 07 4:05 PM Page 294 CHAPTER 13 s USING DATASETS AND DATA ADAPTERS How It Works You add an UPDATE statement and change the name of the original query string variable from sql to upd in order to clearly distinguish it from this statement // SQL to update employees string upd = @" update employees... Out: Propagating New Dataset Rows to a Data Source In this example, you’ll again modify ModifyDataTable.cs (Listing 13-4) to delete a row from the database 1 Add a new C# Console Application project named PersistDeletes to your Chapter13 solution Rename Program.cs to PersistDeletes.cs 2 Replace the code in PersistDeletes.cs with the code in Listing 13 -7 Listing 13 -7 PersistDeletes.cs using System;... propagated to the database The city for employee Steven Buchanan is now Wilmington, not London InsertCommand Property The data adapter uses the InsertCommand property for inserting rows into a table Upon calling the Update method, all rows added to the data table will be searched for and propagated to the database Try It Out: Propagating New Dataset Rows to a Data Source Let’s propagate a new row to the... string variable from sql to del in order to clearly distinguish it from this statement) // SQL to delete employees string del = @" delete from employees where employeeid = @employeeid "; You insert the DELETE code ahead of the display After creating a command and mapping a parameter: // delete employees // // create command SqlCommand cmd = new SqlCommand(del, conn); 9004ch13final.qxd 12/13/ 07 4:05 PM Page... Tip in any editor, or even use the type or more commands to view it from the command line How It Works You replace a console display loop with a method call to write the XML file // extract dataset to XML file ds.WriteXml( @" C:\Documents and Settings\Administrator\My Documents\Visual Studio Codename Orcas\Projects\Chapter13\productstable.xml" ); You give the full path for the XML file to place it in... SelectCommand property to extract metadata for the database table If any changes are made to the SelectCommand property after invoking the Update method, you should call the RefreshSchema method on the command builder to refresh the metadata accordingly To create a command builder, you create an instance of the data provider’s command builder class, passing a data adapter to its constructor For example,... you’ve been assuming that no 9004ch13final.qxd 12/13/ 07 4:05 PM Page 311 CHAPTER 13 s USING DATASETS AND DATA ADAPTERS other changes have been made to the database while you’ve been working with disconnected datasets Imagine two separate users trying to make conflicting changes to the same row in a dataset and then trying to propagate these changes to the database What happens? How does the database... Figure 13-11 Figure 13-11 Extracting a data table as XML 313 9004ch13final.qxd 314 12/13/ 07 4:05 PM Page 314 CHAPTER 13 s USING DATASETS AND DATA ADAPTERS 4 Not much seems to have happened, but that’s because you wrote to a file rather than to the screen Open productstable.xml to see the XML (One way in Visual Studio is to use File ® Open File.) Figure 13-12 shows the XML extracted for the first five product... provider has its own command builder If a data table corresponds to a single database table, you can use a command builder to automatically generate the appropriate UpdateCommand, InsertCommand, and DeleteCommand properties for a data adapter This is all done transparently when a call is made to the data adapter’s Update method To be able to dynamically generate INSERT, DELETE, and UPDATE statements,... in the solution directory Were you to give only the file name, it would have been placed in the bin\Release subdirectory under the WriteXML project directory 9004ch13final.qxd 12/13/ 07 4:05 PM Page 315 CHAPTER 13 s USING DATASETS AND DATA ADAPTERS Note that the XML has simply mapped the dataset as a hierarchy The first XML element, , is the dataset name (defaulting to NewDataSet since you . to persist the original data source changes made to a dataset. ■Note Changes you make to a dataset aren’t automatically propagated to a database. To save the changes in a database, you need to. original query string variable from sql to ins in order to clearly distinguish it from this statement. // SQL to insert employees string ins = @" insert into employees ( firstname, lastname, titleofcourtesy, city, country ) values ( @firstname, @lastname, @titleofcourtesy, @city, @country ) "; CHAPTER. and change the name of the original query string variable from sql to upd in order to clearly distinguish it from this statement. // SQL to update employees string upd = @" update employees set city

Ngày đăng: 08/08/2014, 18:21

TỪ KHÓA LIÊN QUAN