What I want to talk about in this section is the part that everyone is very concerned about and must be very interested in. Hehe, it is also the part that I am best at writing tutorials-ADO database access. Thinking of this, I remember what I wrote last year "Accessing the Database" series of articles, hehe! So, if you find it difficult to understand things like record sets, I recommend you to read my articles first. Okay, 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 is just a modern Some data access technologies are integrated. If you don’t understand ADO, think about ODBC! In fact, when we were doing asp, we used ADO. Do you remember that set that we have used countless times? conn=Server.CreateObject("ADODB.Connection")? Yes, that's it. As for some conceptual things about ADO, please check the information yourself. However, it doesn't matter if you don't understand it. Just imagine it as a M$ give us tools to access data!
OK, the following example is based on a M$ access 2000 database. Its structure is as follows. The table name is Categories and the file name is BugTypes.mdb. Hurry up and create it. One:
Category ID Category Name
1 Bugbash stuff
2 Appweek Bugs
3 .NET Reports
4 Internal support
Okay, I’ll write out all the programs first, and then let’s taste them sentence by sentence:
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: // 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";
012:
013: //Create a record set and fill in the Categories table
014: DataSet myDataSet = new DataSet();
015: myDataSet.Tables.Add( "Categories");
016:
017: //Create ADO instance 018: ADOConnection myAccessConn = new ADOConnection(strAccessConn);
019: ADODataSetCommand myAccessDataSetCmd = new ADODataSetCommand();
020: myAccessDataSetCmd.SelectCommand = new ADOCom mand(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 into 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 lines of programs show two methods of getting the number of tables in a data set 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 lines of program illustrate how to obtain information from a record set by relying on the name of the table
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 column subscript, column name and data type053: 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, just because I chose the example poorly, haha. However, after a careful analysis, it is still understandable. I am now Let’s talk about a few special things in this example. The first is that unlike in ASP, a command string must be treated as a command object. 020 does exactly this. Note that there is a myDataSet in line 015 .Tables.Add("Categories") statement, this is not filling in the Categories table in the database, it is just creating an empty table, and 025 is the real filling 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
Okay, that’s all, SUNWEN is really I want to sleep, and no music works anymore, haha. It takes a lot of effort to fully understand this example. O.K.886!
The above is the content of the SUNWEN tutorial - C# Advanced (8), more For related content, please pay attention to the PHP Chinese website (www.php.cn)!