C#中Dataset的作用是什么

发布时间:2021-06-15 17:30:01 作者:Leah
来源:亿速云 阅读:344

这篇文章将为大家详细讲解有关C#中Dataset的作用是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

新建一 WEB 项目,创建一业务类:操作C# Dataset代码如下所示:

  1. using System;  

  2. using System.Data;  

  3. using System.Configuration;  

  4. using System.Web;  

  5. using System.Web.Security;  

  6. using System.Web.UI;  

  7. using System.Web.UI.WebControls;  

  8. using System.Web.UI.WebControls.WebParts;  

  9. using System.Web.UI.HtmlControls;  

  10. using System.Data.OleDb;  

  11. using System.Text;  

  12.  

  13. /**//// <summary> 

  14. /// test 的摘要说明  

  15. /// </summary> 

  16. public class test  

  17. {  

  18. public test()  

  19. {  

  20. //  

  21. // TODO: 在此处添加构造函数逻辑  

  22. //  

  23. }  

  24.  

  25. // 数据集传递测试  

  26. [Ajax.AjaxMethod()]  

  27. public DataSet GetDataSet()  

  28. {  

  29. OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;

  30. Data Source=D:工作项目分析 estdb.mdb;Persist Security Info=True;");  

  31. DataSet ds = new DataSet();  

  32. try  

  33. {  

  34. OleDbCommand cmd = conn.CreateCommand();  

  35. cmd.CommandText = "select * from t_name";  

  36. cmd.CommandType = CommandType.Text;  

  37.  

  38. OleDbDataAdapter da = new OleDbDataAdapter(cmd);  

  39. da.Fill(ds);  

  40. return ds;  

  41. }  

  42. catch  

  43. {  

  44. conn.Close();  

  45. throw;  

  46. }  

  47. }  

  48. }创建好业务类以后,再新建一 Aspx 页面,在页面的 Page_Load 中注册业务类: 
    protected void Page_Load(object sender, EventArgs e)  

  49. {  

  50. Ajax.Utility.RegisterTypeForAjax(typeof(test));  

  51. }页面代码如下: <%@ Page Language="C#" AutoEventWireup="true" 
    CodeFile="read_dataset.aspx.cs" Inherits="read_dataset" %> 

  52.  

  53. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

  54. <html xmlns="http://www.w3.org/1999/xhtml"> 

  55. <head runat="server"> 

  56. <title>read_dataset</title> 

  57.  

  58. <script language="JavaScript"> 

  59.  

  60. function getDataSet()  

  61. {  

  62. var ds =test.GetDataSet().value;  

  63. if(ds != null && typeof(ds) == "object" && ds.Tables != null)  

  64. {  

  65. var s = new Array();  

  66. s[s.length] = "<table style='border: #000000 1px solid; color: #993333; 

  67. font-family: 'Microsoft Sans Serif'; background-color: #ffff99;'>";  

  68.  

  69. for(var i=0; i<ds.Tables[0].Rows.length; i++)  

  70. {  

  71. s[s.length] = "<tr>";  

  72. s[s.length] = "<td>" + ds.Tables[0].Rows[i].id + "</td>";  

  73. s[s.length] = "<td>" + ds.Tables[0].Rows[i].f_date + "</td>";  

  74. s[s.length] = "<td>" + ds.Tables[0].Rows[i].f_name + "</td>";  

  75. s[s.length] = "</tr>";  

  76. }  

  77.  

  78. s[s.length] = "</table>";  

  79. document.getElementById("div1").innerHTML = s.join("");  

  80. }  

  81. else  

  82. {  

  83. alert("调用Ajax接口函数错误!");  

  84. }  

  85. }  

  86. </script> 

  87.  

  88. </head> 

  89. <body> 

  90. <form id="form1" runat="server"> 

  91. <input type="button" value="dataset" onclick="getDataSet();" /> 

  92. <div id="div1"> 

  93. </div> 

  94. </form> 

  95. </body> 

  96. </html> 

关于C#中Dataset的作用是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. C#中DataSet有什么作用
  2. C#中AppDomain的作用是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

dataset

上一篇:如何理解php PSR-4

下一篇:使用EasyPOI怎么实现动态生成列数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》