自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#創(chuàng)建DataSet對象詳細(xì)介紹

開發(fā) 后端
這里介紹如何C#創(chuàng)建DataSet對象,以及如何使用WriteXML方法將該對象包含的數(shù)據(jù)導(dǎo)出到 XML 文件中。生成的 XML 文件可以直接在 Excel 中打開。

C#語言有很多值得學(xué)習(xí)的地方,這里我們主要介紹C#創(chuàng)建DataSet對象,包括介紹在視圖菜單上,選擇工具箱以顯示“工具箱”等方面。

本文說明如何C#創(chuàng)建DataSet對象,以及如何使用WriteXML方法將該對象包含的數(shù)據(jù)導(dǎo)出到 XML 文件中。生成的 XML 文件可以直接在 Excel 中打開。為便于說明,使用 Jet OLEDB 提供程序從 Microsoft Access Northwind 示例數(shù)據(jù)庫C#創(chuàng)建DataSet對象。但是,類似的代碼可與您使用 Visual C#創(chuàng)建DataSet對象一起使用。

1. 啟動 Microsoft Visual Studio .NET。在文件菜單上,單擊新建,然后單擊項目。從 Visual C# 項目類型中選擇Windows 應(yīng)用程序。默認(rèn)情況下創(chuàng)建 Form1。

2. 在視圖菜單上,選擇工具箱以顯示“工具箱”,然后向 Form1 中添加一個按鈕。

3. 雙擊Button1。將出現(xiàn)該窗體的代碼窗口。

4. 將下面的using 指令添加到 Form1.cs 頂部:

  1. using System.Data.OleDb;  
  2. using System.Xml; 

5.將下面的私有成員變量添加到 Form1 類中:

  1. private string strConn =Provider=Microsoft.Jet.OLEDB.4.0;  
  2. Data Source= + C:\\Program Files\\Microsoft Office
  3. \\Office10\\Samples\\ + Northwind.mdb;; 

注意:您可能需要修改連接字符串中 Northwind.mdb 的路徑,以便與您安裝的位置相匹配。

6.在button1_Click 處理程序中添加以下代碼:

  1. //Connect to the data source.OleDbConnection objConn = 
    new OleDbConnection (strConn);  
  2. try{  
  3. objConn.Open();  
  4. //Fill a dataset with records from the Customers table.OleDbCommand objCmd = 
    new OleDbCommand(Select CustomerID, CompanyName, ContactName+ Country, 
    Phone from Customers, objConn);  
  5. OleDbDataAdapter objAdapter = new OleDbDataAdapter();  
  6. objAdapter.SelectCommand = objCmd;  
  7. DataSet objDataset = new DataSet();  
  8. objAdapter.Fill(objDataset);  
  9. //Create the FileStream to write with.System.IO.FileStream fs = 
    new System.IO.FileStream(C:\\Customers.xml,System.IO.FileMode.Create);  
  10. //Create an XmlTextWriter for the FileStream. System.Xml.XmlTextWriter xtw = 
    new System.Xml.XmlTextWriter(fs, System.Text.Encoding.Unicode);  
  11. //Add processing instructions to the beginning of the XML file, one  
  12. //of which indicates a style sheet.xtw.WriteProcessingInstruction
    (xml, 
    version='1.0');  
  13. //xtw.WriteProcessingInstruction(xml-stylesheet,  
  14. // type='text/xsl' href='customers.xsl');  
  15. //Write the XML from the dataset to the file.objDataset.WriteXml(xtw);  
  16. xtw.Close();  
  17. //Close the database connection.  
  18. objConn.Close();  
  19. }  
  20. catch (System.Exception ex)  
  21. {  
  22. MessageBox.Show(ex.Message);  

【編輯推薦】

  1. C#使用sqlserver存儲淺析
  2. 由C++轉(zhuǎn)向C#簡單介紹
  3. C# oledbconnection方法淺談
  4. 數(shù)據(jù)庫常用C#代碼概述
  5. C#調(diào)用ImOK學(xué)習(xí)筆記
責(zé)任編輯:佚名 來源: 博客園
相關(guān)推薦

2009-08-12 15:43:02

操作C# Datase

2009-08-27 17:31:44

C#創(chuàng)建Windows

2009-08-10 16:30:56

C# BitmapDa

2009-08-12 15:34:40

C# DBNull

2009-08-21 15:16:23

C#使用指針

2009-08-26 17:31:59

C# const常量

2009-08-24 18:21:23

C# ListView

2009-08-03 18:49:17

C#和Java

2009-08-20 15:26:42

C#循環(huán)語句

2009-08-21 09:23:11

C# GDI+

2009-08-07 16:10:20

C#調(diào)用API

2009-08-13 13:38:30

C#命名規(guī)范

2009-08-14 17:04:50

C#類型系統(tǒng)

2009-08-27 14:32:15

C#編寫ActiveX

2009-08-06 14:59:36

C#編譯器

2009-08-13 15:40:28

C#基礎(chǔ)知識

2011-06-08 13:35:18

C#數(shù)據(jù)類型

2009-09-03 09:40:57

C#創(chuàng)建表單

2009-09-03 17:21:51

C# VSProjec

2009-08-10 13:40:46

創(chuàng)建C# COM對象
點贊
收藏

51CTO技術(shù)棧公眾號