The----of the Sunwen Tutorial C # advanced 8

Source: Internet
Author: User
Tutorial Sunwen Tutorial----C # Advanced
(eight)
Mrfat@china.com
Hello everybody, I am Wuhan Hua Division's Sunwen, today zzz .... Come on, wake up!!.. Don't call me up and don't use leather shoes to @#$%&*. It is May 4 night 19:33, I really want to sleep. I had to turn on the music to refresh myself!

This section I want to talk about is very concerned about, certainly is also very interested in the part. Hey, it's the best part of my tutorial----ADO database access. Thinking about it, I think of the "Access database" series I wrote last year, hehe! So, If you think it's hard to understand a recordset or something, I recommend that you take a look at my articles first. All right, let's get started!

What is ADO (ActiveX Data Objects), ADO is a very simple idea, an idea that lets you access data in just one way. ADO is not a new idea, only the use of existing data access technology, to integrate it. If you don't understand ADO, think about ODBC! In fact, we are engaged in the ASP when the use of ADO, remember, that once we have used countless times set conn= Server.CreateObject ("ADODB.") Connection ")? Yes, that's it. As for some of the conceptual things ADO, please refer to the information, but, in fact, do not understand it does not matter, just think of it as a m$ to our access to Data Tools bar!

OK, the following example is based on a m$ ACCESS 2000 database, its structure is as follows, the table name is categories, the filename is Bugtypes.mdb, quickly build a bar:

Category ID Category Name
1 Bugbash Stuff
2 Appweek Bugs
3. NET Reports
4 Internal Support

OK, I'll write all the procedures first, and then we'll taste the sentence by sentence:

M://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://Set good connection string and select command string 010:string straccessconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=bugtypes.mdb ";
011:string straccessselect = "SELECT * from Categories";
012:
013://Set up the Recordset, and fill in the Categories this form
014:dataset myDataSet = new DataSet ();
015:mydataset.tables.add ("Categories");
016:
017://Establish ADO instance 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://A Recordset can contain multiple tables, we put them in an array 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://The following two-line program shows two ways to get the number of tables in this dataset from one recordset
042:console.writeline ("{0} tables in data set", MyDataSet.Tables.Count);
043:console.writeline ("{0} tables in data set", DTA.) Length);
044://The following lines show how to rely on the name of a table to get information from a recordset
045:console.writeline ("{0} rows in Categories table", mydataset.tables["Categories"). Rows.Count);
046://Column information is automatically obtained from the database, so we can use the following code 047:console.writeline ("{0} columns in Categories table", mydataset.t ables["Categories"]. Columns.count);
048:datacolumn[] DRC = mydataset.tables["Categories"]. Columns.all;
049:int i = 0;
050:foreach (DataColumn DC in DRC)
051: {
052://Print out the subscript and 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:\n{0} ", E.message);
065:}
066:}
067:}
It seems that this example is a bit complicated, I only blame my example to choose not good, hehe. However, it is understandable to analyze it carefully. I'm going to talk about some of the special things in this example. The first is not like in ASP, A command string is required to be treated as a Command object. 020 That's what this is about. Note that there is a myDataSet.Tables.Add ("Categories") statement in line 015, which does not populate the Categories table in the database, It's just an empty table, and 025 is the real fill of data.

The output of 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, that's it, Sunwen really want to sleep, what music is no use, hehe. This example really takes a lot of effort to fully understand. o.k.886!

Next page


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.