SUNWEN tutorial ---- C # advanced (8)

Source: Internet
Author: User

Hello, everyone. I am SUNWEN from Wuhan Hashi. Today, Zzz... (wake up! Pop !.. Pop! I don't need to use my shoes for shoes. @ # $ % & *) It's May 4. I really want to go to bed. I have to turn on the music to make me feel refreshed!

What I want to talk about in this section is something that everyone cares about and is certainly very interested in. hey hey, it's also the best part of my tutorial-ADO database access. this reminds me of the "database access" series I wrote last year! So, if you think it is hard to understand the record set or something like that, I recommend you read my articles first. Well, let's get started!

What is ADO (ActiveX Data Objects translated as ActiveX Data Objects), ADO is a very simple idea, an idea that allows you to access Data in only one way. ADO is not a new idea. It only uses the existing data access technology to integrate it. if you don't understand ADO, think about ODBC! In fact, we used ADO when we were engaged in ASP. Remember the set conn = Server that we used countless times. createObject ("ADODB. connection? Yes, it is. for some conceptual items of ADO, please refer to the materials on your own. However, it doesn't matter if you don't know it. Just think of it as a tool for accessing data from M $!

OK. The following example is based on a database of M $ ACCESS 2000. Its structure is as follows. The table name is Categories and the file name is BugTypes. mdb. Please create one quickly:

Category ID Category Name
1 Bugbash stuff
2 Appweek Bugs
3. NET Reports
4 Internal support

Okay. I will write all the programs first, and then let's try it with one sentence:

000: // ADOadosample. cs
001: using System;
002: using System. Data;
003: using System. Data. ADO;
005: public class MainClass
006 :{
007: public static void Main ()
008 :{
009: // set the connection string and select the command string 010: string strAccessConn = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = BugTypes. MDB ";
011: string strAccessSelect = "SELECT * FROM Categories ";
013: // create a record set and enter the Categories table
014: DataSet myDataSet = new DataSet ();
015: myDataSet. Tables. Add ("Categories ");
017: // create An ADO instance 018: ADOConnection myAccessConn = new ADOConnection (strAccessConn );
019: ADODataSetCommand myAccessDataSetCmd = new ADODataSetCommand ();
020: myAccessDataSetCmd. SelectCommand = new ADOCommand (strAccessSelect, myAccessConn );
022: myAccessConn. Open ();
023: try
024 :{
025: myAccessDataSetCmd. FillDataSet (myDataSet, "Categories ");
026 :}
027: finally
028 :{
029: myAccessConn. Close ();
030 :}
032: try
033 :{
034: // a record set can contain multiple Tables. We put them in an array 035: able [] dta = myDataSet. Tables. All;
036: foreach (DataTable dt in dta)
037 :{
038: Console. WriteLine ("Found data table {0}", dt. TableName );
039 :}
041: // The following two rows of programs demonstrate two ways to get the number of tables in this dataset from a record set
042: Console. WriteLine ("{0} tables in data set", myDataSet. Tables. Count );
043: Console. WriteLine ("{0} tables in data set", dta. Length );
044: // The following procedures explain how to obtain information from a record set by table name
045: Console. WriteLine ("{0} rows in Categories table", myDataSet. Tables ["Categories"]. Rows. Count );
046: // The column information is automatically obtained from the database, so we can use the following code 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: // print the subscript of the column and the column name and Data Type 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: // print out CategoryID and 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 :}
It seems that this example is somewhat complicated, but it is not a good choice for me. however, it is understandable to analyze it in detail. let me talk about some special things in this example. first, unlike ASP, a command string must be treated as a command object. 020 is doing exactly this. note that there is a myDataSet in row 015. tables. add ("Categories") statement. This is not to fill in the database's Categories table, but to create an empty table, and 025 is to fill the data.

The output in this example is:

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
Well, let's talk about it. SUNWEN really wants to sleep, and no music is used. Well, this example really takes a lot of effort to fully understand. O. K.886!

Related Article

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: 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.