1 Bugbash stuff
2 Appweek Bugs
3 .NET Reports
4 Internal support
好的,我先把所有的程序都写出来,然后我们来一句一句的品尝:
000: // ADOadosample.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: //建立记录集,并把Categories这个表填进去
014: DataSet myDataSet = new DataSet();
015: myDataSet.Tables.Add("Categories");
016:
017: //建立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: //下面的两行程序展示了两种从一个记录集中得到这个数据集中的表格数的方法
042: Console.WriteLine("{0} tables in data set", myDataSet.Tables.Count);
043: Console.WriteLine("{0} tables in data set", dta.Length);
044: //下面的几行程序说明了如何从一个记录集中依靠表格的名称来取得信息
045: Console.WriteLine("{0} rows in Categories table", myDataSet.Tables["Categories"].Rows.Count);
046: //列的信息是自动从数据库中得到的,所以我们可以用以下的代码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: //打印出列的下标和列的名称和数据类型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: //打印出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:{0}", e.Message);
065: }
066: }
067: }
看起来,这个例子是有一些复杂的,只怪我例子选的不好,呵呵.不过,细细分析一下,还是可以理解的.我现在说一下这个例子中几个特别的东东.第一就是不象在ASP中,一个命令字符串被须被当作一个命令对象.020做的正是这个事情.注意,在015行有一个myDataSet.Tables.Add("Categories")语句,这并不是把数据库中的Categories这个表填进去,只不过是建一个空表,而025才是真的填充数据.
这个例子的输出是:
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
好了,就说到这吧,SUNWEN真是想睡了,什么音乐都不管用了,呵呵.这个例子还真要花大力气才能完全理解.O.K.886!