扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
Category ID Category Name 1 Bugbash stuff 2 Appweek Bugs 3 .NET Reports 4 Internal support |
000: // ADO\adosample.cs 001: using System; 002: using System.Data; 003: using System.Data.ADO; 004: 005: public class MainClass 006: { 007: public static void Main () 008: { 009: // 设定好连接字符串和选择命令字符串010: string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BugTypes.MDB"; 011: string strAccessSelect = "SELECT * FROM Categories"; 012: 013: file://建立记录集,并把Categories这个表填进去 014: DataSet myDataSet = new DataSet(); 015: myDataSet.Tables.Add("Categories"); 016: 017: file://建立ADO实例018: ADOConnection myAccessConn = new ADOConnection(strAccessConn); 019: ADODataSetCommand myAccessDataSetCmd = new ADODataSetCommand(); 020: myAccessDataSetCmd.SelectCommand = new ADOCommand(strAccessSelect,myAccessConn); 021: 022: myAccessConn.Open(); 023: try 024: { 025: myAccessDataSetCmd.FillDataSet(myDataSet,"Categories"); 026: } 027: finally 028: { 029: myAccessConn.Close(); 030: } 031: 032: try 033: { 034: // 一个记录集可以包含多个表,我们把它们放到一个数组中035: DataTable[] dta = myDataSet.Tables.All; 036: foreach (DataTable dt in dta) 037: { 038: Console.WriteLine("Found data table {0}", dt.TableName); 039: } 040: 041: file://下面的两行程序展示了两种从一个记录集中得到这个数据集中的表格数的方法 042: Console.WriteLine("{0} tables in data set", myDataSet.Tables.Count); 043: Console.WriteLine("{0} tables in data set", dta.Length); 044: file://下面的几行程序说明了如何从一个记录集中依靠表格的名称来取得信息 045: Console.WriteLine("{0} rows in Categories table", myDataSet.Tables["Categories"].Rows.Count); 046: file://列的信息是自动从数据库中得到的,所以我们可以用以下的代码047: Console.WriteLine("{0} columns in Categories table", myDataSet.Tables["Categories"].Columns.Count); 048: DataColumn[] drc = myDataSet.Tables["Categories"].Columns.All; 049: int i = 0; 050: foreach (DataColumn dc in drc) 051: { 052: file://打印出列的下标和列的名称和数据类型053: Console.WriteLine("Column name[{0}] is {1}, of type {2}",i++ , dc.ColumnName, dc.DataType); 054: } 055: DataRow[] dra = myDataSet.Tables["Categories"].Rows.All; 056: foreach (DataRow dr in dra) 057: { 058: file://打印出CategoryID和CategoryName059: Console.WriteLine("CategoryName[{0}] is {1}", dr[0], dr[1]); 060: } 061: } 062: catch (Exception e) 063: { 064: Console.WriteLine("Oooops. Caught an exception:\n{0}", e.Message); 065: } 066: } 067: } |
Found data table Categories 1 tables in data set 1 tables in data set 4 rows in Categories table 2 columns in Categories table Column name[0] is CategoryID, of type Int32 Column name[1] is CategoryName, of type System.String CategoryName[1] is Bugbash stuff CategoryName[2] is Appweek Bugs CategoryName[3] is .NET Reports CategoryName[4] is Internal support |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者