c# code 39 Updating a DataSet by Using SqlXmlAdapter in Visual C#.NET

Generate USS Code 39 in Visual C#.NET Updating a DataSet by Using SqlXmlAdapter

Updating a DataSet by Using SqlXmlAdapter
Draw Code 39 Extended In Visual C#.NET
Using Barcode generator for Visual Studio .NET Control to generate, create USS Code 39 image in Visual Studio .NET applications.
www.OnBarcode.com
Decode Code 39 Extended In C#.NET
Using Barcode reader for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
In the preceding example, we simply populated a DataSet with the help of the SqlXmlAdapter class. What if you make changes to the DataSet data and wish to save those changes in the database The SqlXmlAdapter does provide the Update() method that updates the database with any changes to your DataSet. However, you need to do a bit more work than that. While filling the DataSet, you need to specify the XSD schema for the DataTable being created. This schema provides mapping between the DataTable column names and the actual table column names. In our example, we retrieve three columns of the Employee table: EmployeeID, FirstName, and LastName. The schema for this data structure is shown in Listing 10-21. Listing 10-21. Schema Employees.xsd for Our Data < xml version="1.0" encoding="utf-8" > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Employees"> <xs:complexType> <xs:sequence> <xs:element name="EmployeeID" type="xs:integer"/> <xs:element name="FirstName" type="xs:string"/> <xs:element name="LastName" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> The schema defines a root element called <Employees>, which has three child elements: <EmployeeID>, <FirstName>, and <LastName>. Note that the schema defines the columns as elements and not as attributes. To see the SqlXmlAdapter class in action, you need to develop an application as shown in Figure 10-5.
2D Encoder In C#.NET
Using Barcode drawer for VS .NET Control to generate, create 2D Barcode image in .NET framework applications.
www.OnBarcode.com
Draw PDF-417 2d Barcode In C#
Using Barcode generation for Visual Studio .NET Control to generate, create PDF417 image in VS .NET applications.
www.OnBarcode.com
CHAPTER 10 XML IN S QL SERVE R
Draw Linear 1D Barcode In Visual C#.NET
Using Barcode creator for VS .NET Control to generate, create Linear image in .NET applications.
www.OnBarcode.com
Barcode Printer In Visual C#.NET
Using Barcode maker for Visual Studio .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Figure 10-5. Application for illustrating the Update() method of SqlXmlAdapter The application consists of a DataGridView control that displays all the employees from the Employees table. You can change the data in the DataGridView and click the Update button to save the changes back to the database. The complete code that makes this application work is shown in Listing 10-22. Listing 10-22. Saving Changes Made to a DataSet DataSet ds = new DataSet(); SqlXmlAdapter da; SqlXmlCommand cmd; string strConn = @"Provider=SQLOLEDB;server=.\sqlexpress;database=northwind;integrated security=SSPI"; private void Form1_Load(object sender, EventArgs e) { cmd = new SqlXmlCommand(strConn); cmd.RootTag = "ROOT"; cmd.CommandText = "Employees"; cmd.CommandType = SqlXmlCommandType.XPath; cmd.SchemaPath = Application.StartupPath + @"\employees.xsd"; ds = new DataSet(); da = new SqlXmlAdapter(cmd); da.Fill(ds); dataGridView1.DataSource = ds.Tables[0].DefaultView; }
QR Code ISO/IEC18004 Generator In Visual C#.NET
Using Barcode maker for VS .NET Control to generate, create QR-Code image in VS .NET applications.
www.OnBarcode.com
Identcode Creation In C#.NET
Using Barcode creation for VS .NET Control to generate, create Identcode image in .NET applications.
www.OnBarcode.com
CHAPTER 10 XML IN S QL SERVE R
Code 39 Full ASCII Scanner In Visual Basic .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Encoding Code39 In Java
Using Barcode creator for Java Control to generate, create Code 39 Extended image in Java applications.
www.OnBarcode.com
private void button1_Click(object sender, EventArgs e) { da.Update(ds); } The code in Listing 10-22 shows several interesting things. The SqlXmlCommand, DataSet, and SqlXmlAdapter variables are declared at the form level because we will be using them in more than one place. Notice the code marked in bold. It sets the RootTag property of the SqlXmlCommand property. The AUTO mode of the FOR XML clause doesn t return data along with a root element by default, so this property is used to indicate the name of the root element inside which the rest of the XML data will be wrapped. The CommandType property is set to XPath, indicating that the CommandText property is an XPath expression. This means that this time the CommandText property is not a SELECT query but the XPath expression Employees, which will return various <Employees> elements. The CommandType property of the SqlXmlCommand class is of type SqlXmlCommandType. The possible values of the SqlXmlCommandType enumeration are listed in Table 10-4.
Encoding USS Code 39 In None
Using Barcode maker for Software Control to generate, create Code 39 Full ASCII image in Software applications.
www.OnBarcode.com
Draw PDF-417 2d Barcode In None
Using Barcode generator for Online Control to generate, create PDF-417 2d barcode image in Online applications.
www.OnBarcode.com
Table 10-4. Values of the SqlXmlCommandType Enumeration
EAN 13 Printer In None
Using Barcode drawer for Software Control to generate, create EAN / UCC - 13 image in Software applications.
www.OnBarcode.com
Paint UPCA In Java
Using Barcode printer for Java Control to generate, create GTIN - 12 image in Java applications.
www.OnBarcode.com
Value
Making UCC.EAN - 128 In Java
Using Barcode encoder for BIRT Control to generate, create UCC-128 image in BIRT applications.
www.OnBarcode.com
Drawing UPC-A Supplement 5 In Java
Using Barcode generator for BIRT reports Control to generate, create UPCA image in Eclipse BIRT applications.
www.OnBarcode.com
DiffGram Sql Template TemplateFile UpdateGram XPath
Barcode Printer In Visual Basic .NET
Using Barcode maker for .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
Draw Barcode In None
Using Barcode encoder for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Description
QR Code Generation In None
Using Barcode maker for Software Control to generate, create QR-Code image in Software applications.
www.OnBarcode.com
Code 128 Code Set B Maker In Java
Using Barcode generation for Java Control to generate, create Code-128 image in Java applications.
www.OnBarcode.com
Indicates that CommandText is a DiffGram Indicates that CommandText is a SQL statement (default) Indicates that CommandText is a template Indicates that CommandText is a template file Indicates that CommandText is an UpdateGram Indicates that CommandText is a valid XPath expression
Further, the SchemaPath property specifies the path of the schema file that we created earlier. Then the SqlXmlAdapter populates a DataSet, which is bound to the DataGridView. After the data is displayed in the DataGridView, you can modify it. After the modifications are complete, you need to click the Update button. The Click event of the Update button calls the Update() method of SqlXmlAdapter, which accepts the DataSet whose changes are to be reflected in the database. In 7, you learned that the DataSet class internally tracks the changes made to the data by using the DiffGram format. The same DiffGram is used by the SqlXmlAdapter class to propagate the changes back to the database.
Copyright © OnBarcode.com . All rights reserved.