程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 轉載C# Tutorials,ADO\adosample.cs

轉載C# Tutorials,ADO\adosample.cs

編輯:關於C語言
using System;
using System.Data;
using System.Data.ADO;

public class MainClass
{
   public static void Main ()
   {
      // set Access connection and select strings
      string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BugTypes.MDB";
      string strAccessSelect = "SELECT * FROM CategorIEs";

      //Create the dataset and add the CategorIEs table to it
      DataSet myDataSet = new DataSet();
      myDataSet.Tables.Add("CategorIEs");
      
      // create my Access objects
      ADOConnection myAccessConn = new ADOConnection(strAccessConn);
      ADODataSetCommand myAccessDataSetCmd = new ADODataSetCommand();
      myAccessDataSetCmd.SelectCommand = new ADOCommand(strAccessSelect,myAccessConn);

      myAccessConn.Open();
      try
      {
         myAccessDataSetCmd.FillDataSet(myDataSet,"CategorIEs");
      }
      finally
      {
         myAccessConn.Close();
      }

      try
      {
         /* A dataSet can contain multiple tables,
           so let's get them all into an array */
         DataTable[] dta = myDataSet.Tables.All;
         foreach (DataTable dt in dta)
         {
            Console.WriteLine("Found data table {0}", dt.TableName);
         }
         
         /* The next two lines show two different ways
            you can get the count of tables in a dataset */
         Console.WriteLine("{0} tables in data set", myDataSet.Tables.Count);
         Console.WriteLine("{0} tables in data set", dta.Length);
         /* The next several lines show how to get information
            on a specific table by name from the dataset */
         Console.WriteLine("{0} rows in Categories table", myDataSet.Tables["CategorIEs"].Rows.Count);
         /* The column info is automatically fetched from the
            database, so we can read it here */
         Console.WriteLine("{0} columns in Categories table", myDataSet.Tables["CategorIEs"].Columns.Count);
         DataColumn[] drc = myDataSet.Tables["CategorIEs"].Columns.All;
         int i = 0;
         foreach (DataColumn dc in drc)
         {
            /* Print the column subscript, then the
               column's name and its data type */
            Console.WriteLine("Column name[{0}] is {1}, of type {2}",i++ , dc.ColumnName, dc.DataType);
         }
         DataRow[] dra = myDataSet.Tables["CategorIEs"].Rows.All;
         foreach (DataRow dr in dra)
         {
            /* Print the CategoryID as a subscript,
               then the CategoryName */
            Console.WriteLine("CategoryName[{0}] is {1}", dr[0], dr[1]);
         }
      }
      catch (Exception e)
      {
         Console.WriteLine("Oooops.  Caught an exception:\n{0}", e.Message);
      }
   }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved