C#中怎么創(chuàng)建一個(gè)DataSet對(duì)象

C#中怎么創(chuàng)建一個(gè)DataSet對(duì)象,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

成都創(chuàng)新互聯(lián)10多年成都企業(yè)網(wǎng)站定制服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及高端網(wǎng)站定制服務(wù),成都企業(yè)網(wǎng)站定制及推廣,對(duì)隧道混凝土攪拌車(chē)等多個(gè)方面擁有豐富的網(wǎng)站推廣經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。

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

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

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

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

using System.Data.OleDb;  using System.Xml;

5.將下面的私有成員變量添加到 Form1 類(lèi)中:

private string strConn =Provider=Microsoft.Jet.OLEDB.4.0;  Data Source= + C:\\Program Files\\Microsoft Office\\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);  

關(guān)于C#中怎么創(chuàng)建一個(gè)DataSet對(duì)象問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

文章名稱(chēng):C#中怎么創(chuàng)建一個(gè)DataSet對(duì)象
地址分享:http://muchs.cn/article18/isjcgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、App開(kāi)發(fā)外貿(mào)建站、微信小程序、網(wǎng)站改版、微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)