Storing XML to a Database Field
Số trang: 5
Loại file: pdf
Dung lượng: 18.65 KB
Lượt xem: 5
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:
[ Team LiB ] Recipe 8. Storing XML to a Database Field Problem You need to store XML to a field in a database. Solution Store the contents of the InnerXml of the XmlDocument to the database. You can later load this into an empty XmlDocument with LoadXml( ).
Nội dung trích xuất từ tài liệu:
Storing XML to a Database Field [ Team LiB ]Recipe 8. Storing XML to a Database FieldProblemYou need to store XML to a field in a database.SolutionStore the contents of the InnerXml of the XmlDocument to the database. You can laterload this into an empty XmlDocument with LoadXml( ).The schema of table TBL0804 used in this solution is shown in Table 8-4. Table 8-4. TBL0804 schema Column name Data type Length Allow nulls?Id int 4 NoXmlField nvarchar 4000 YesThe sample code contains five event handlers:Form.Load Sets up the DataTable that contains the text field, XmlField, containing the XML and the DataAdapter for the table.Write Button.Click Adds or updates a record in the table with the Id and XmlField entered by the user.Read Button.Click Loads the XML for the specified Id into an XmlDocument and displays it on the form in the XmlField text box.Sample Button.Click Generates sample XML data from the Orders table in Northwind and displays it on the form in the XmlField text box.Clear Button.Click Clears the contents of the Id and XmlField text boxes on the form.The C# code is shown in Example 8-7.Example 8-7. File: StoreXmlFieldForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Windows.Forms;using System.IO;using System.Xml;using System.Data;using System.Data.SqlClient;private DataTable dt;private SqlDataAdapter da;private const String TABLENAME = TBL0804;// . . .private void StoreXmlFieldForm_Load(object sender, System.EventArgs e){ String selectText = SELECT Id, XmlField FROM + TABLENAME; String insertText = INSERT + TABLENAME + (Id, XmlField) + VALUES (@Id, @XmlField); String updateText = UPDATE + TABLENAME + + SET XmlField = @XmlField + WHERE Id = @Id; // Create the data adapter. da = new SqlDataAdapter(selectText, ConfigurationSettings.AppSettings[Sql_ConnectString]); da.UpdateCommand = new SqlCommand(updateText, da.SelectCommand.Connection); da.UpdateCommand.CommandType = CommandType.Text; da.UpdateCommand.Parameters.Add(@Id, SqlDbType.Int, 0, Id); da.UpdateCommand.Parameters.Add(@XmlField, SqlDbType.NText, 0, XmlField); da.InsertCommand = new SqlCommand(insertText, da.SelectCommand.Connection); da.InsertCommand.CommandType = CommandType.Text; da.InsertCommand.Parameters.Add(@Id, SqlDbType.Int, 0, Id); da.InsertCommand.Parameters.Add(@XmlField, SqlDbType.NText, 0, XmlField); dt = new DataTable( ); da.FillSchema(dt, SchemaType.Source); da.Fill(dt);}private void writeButton_Click(object sender, System.EventArgs e){ // Load the ID into variable and text box into XmlDoc. int id = 0; XmlDocument xmlDoc = new XmlDocument( ); try { id = Int32.Parse(idTextBox.Text); xmlDoc.LoadXml(xmlTextBox.Text); } catch(Exception ex) { MessageBox.Show(ERROR: + ex.Message); return; } // Find the row with the ID entered. DataRow row = dt.Rows.Find(new object[] {id}); if(row != null) // For an existing row, update the XmlField. row[XmlField] = xmlDoc.InnerXml; else // For a new row, add the row with the ID and XmlField. dt.Rows.Add(new object[] {id, xmlDoc.InnerXml}); // Update the database using the DataAdapter. da.Update(dt);}private void readButton_Click(object sender, System.EventArgs e){ // Load the ID into variable from text box. int id = 0; try { id = Int32.Parse(idTextBox.Text); } catch(Exception ex) { MessageBox.Show(ERROR: + ex.Message); return; } // Find the row with the ID entered. DataRow row = dt.Rows.Find(new object[] {id}); if(row != null) { // If found, load the XML column value from the row. XmlDocument xmlDoc = new XmlDocument( ); xmlDoc.LoadXml(row[XmlField].ToString( )); // Display the XML. xmlTextBox.Text = xmlDoc.InnerXml; } else xmlTextBox.Text = Record not found for Id = + id;}private void sampleXmlButton_Click(object sender, System.EventArgs e){ DataSet ds = new DataSet( ); // Fill the Categories table and add it to the DataSet. SqlDataAdapter da = new SqlDataAdapter(SELECT TOP 3 * FROM Orders, ConfigurationSettings.AppSettings[Sql_ConnectString]); da.FillSchema(ds, SchemaType.Source, Orders); da.Fill(ds, Orders); // Write the XML for the DataSet to a StringWriter, and output. StringWriter sw = new StringWriter( ); ds.WriteXml(sw, XmlWriteMode.WriteSchema); xmlTextBox.Text = sw.ToString( ); ds.Dispose( ); idTextBox.Clear( );}private void clearButton_Click(object sender, System.EventArgs e){ idTextBox.Clear( ); xmlTextBox.Clear( );}DiscussionThe solution demonstrates how to store XML data in a text field of a database table andsubsequently read it into an XmlDocument using the LoadXml( ) method. Standarddatabase access techniques using a DataAdapter and DataTable are used.[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Storing XML to a Database Field [ Team LiB ]Recipe 8. Storing XML to a Database FieldProblemYou need to store XML to a field in a database.SolutionStore the contents of the InnerXml of the XmlDocument to the database. You can laterload this into an empty XmlDocument with LoadXml( ).The schema of table TBL0804 used in this solution is shown in Table 8-4. Table 8-4. TBL0804 schema Column name Data type Length Allow nulls?Id int 4 NoXmlField nvarchar 4000 YesThe sample code contains five event handlers:Form.Load Sets up the DataTable that contains the text field, XmlField, containing the XML and the DataAdapter for the table.Write Button.Click Adds or updates a record in the table with the Id and XmlField entered by the user.Read Button.Click Loads the XML for the specified Id into an XmlDocument and displays it on the form in the XmlField text box.Sample Button.Click Generates sample XML data from the Orders table in Northwind and displays it on the form in the XmlField text box.Clear Button.Click Clears the contents of the Id and XmlField text boxes on the form.The C# code is shown in Example 8-7.Example 8-7. File: StoreXmlFieldForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Windows.Forms;using System.IO;using System.Xml;using System.Data;using System.Data.SqlClient;private DataTable dt;private SqlDataAdapter da;private const String TABLENAME = TBL0804;// . . .private void StoreXmlFieldForm_Load(object sender, System.EventArgs e){ String selectText = SELECT Id, XmlField FROM + TABLENAME; String insertText = INSERT + TABLENAME + (Id, XmlField) + VALUES (@Id, @XmlField); String updateText = UPDATE + TABLENAME + + SET XmlField = @XmlField + WHERE Id = @Id; // Create the data adapter. da = new SqlDataAdapter(selectText, ConfigurationSettings.AppSettings[Sql_ConnectString]); da.UpdateCommand = new SqlCommand(updateText, da.SelectCommand.Connection); da.UpdateCommand.CommandType = CommandType.Text; da.UpdateCommand.Parameters.Add(@Id, SqlDbType.Int, 0, Id); da.UpdateCommand.Parameters.Add(@XmlField, SqlDbType.NText, 0, XmlField); da.InsertCommand = new SqlCommand(insertText, da.SelectCommand.Connection); da.InsertCommand.CommandType = CommandType.Text; da.InsertCommand.Parameters.Add(@Id, SqlDbType.Int, 0, Id); da.InsertCommand.Parameters.Add(@XmlField, SqlDbType.NText, 0, XmlField); dt = new DataTable( ); da.FillSchema(dt, SchemaType.Source); da.Fill(dt);}private void writeButton_Click(object sender, System.EventArgs e){ // Load the ID into variable and text box into XmlDoc. int id = 0; XmlDocument xmlDoc = new XmlDocument( ); try { id = Int32.Parse(idTextBox.Text); xmlDoc.LoadXml(xmlTextBox.Text); } catch(Exception ex) { MessageBox.Show(ERROR: + ex.Message); return; } // Find the row with the ID entered. DataRow row = dt.Rows.Find(new object[] {id}); if(row != null) // For an existing row, update the XmlField. row[XmlField] = xmlDoc.InnerXml; else // For a new row, add the row with the ID and XmlField. dt.Rows.Add(new object[] {id, xmlDoc.InnerXml}); // Update the database using the DataAdapter. da.Update(dt);}private void readButton_Click(object sender, System.EventArgs e){ // Load the ID into variable from text box. int id = 0; try { id = Int32.Parse(idTextBox.Text); } catch(Exception ex) { MessageBox.Show(ERROR: + ex.Message); return; } // Find the row with the ID entered. DataRow row = dt.Rows.Find(new object[] {id}); if(row != null) { // If found, load the XML column value from the row. XmlDocument xmlDoc = new XmlDocument( ); xmlDoc.LoadXml(row[XmlField].ToString( )); // Display the XML. xmlTextBox.Text = xmlDoc.InnerXml; } else xmlTextBox.Text = Record not found for Id = + id;}private void sampleXmlButton_Click(object sender, System.EventArgs e){ DataSet ds = new DataSet( ); // Fill the Categories table and add it to the DataSet. SqlDataAdapter da = new SqlDataAdapter(SELECT TOP 3 * FROM Orders, ConfigurationSettings.AppSettings[Sql_ConnectString]); da.FillSchema(ds, SchemaType.Source, Orders); da.Fill(ds, Orders); // Write the XML for the DataSet to a StringWriter, and output. StringWriter sw = new StringWriter( ); ds.WriteXml(sw, XmlWriteMode.WriteSchema); xmlTextBox.Text = sw.ToString( ); ds.Dispose( ); idTextBox.Clear( );}private void clearButton_Click(object sender, System.EventArgs e){ idTextBox.Clear( ); xmlTextBox.Clear( );}DiscussionThe solution demonstrates how to store XML data in a text field of a database table andsubsequently read it into an XmlDocument using the LoadXml( ) method. Standarddatabase access techniques using a DataAdapter and DataTable are used.[ Team LiB ]
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Storing XML to a Database FieldGợi ý tài liệu liên quan:
-
52 trang 430 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 314 0 0 -
74 trang 299 0 0
-
96 trang 293 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 289 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 281 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 275 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 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 265 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 265 0 0