Reproduced C # Tutorials,ado\adosample.cs

Source: Internet
Author: User
ADO 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 the them all in 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 I 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);
}
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.