314145 How to: Pack the DataSet object from the database using Visual C # .NET (from mkba)

xiaoxiao2021-03-06  76

The release number of this article has been CHS314145

For Microsoft Visual Basic .NET versions of this article, see

301216.

This article references the following Microsoft .NET Framework Class Bank Name Space:

System.data.sqlclient

This task content

summary

Request to populate the DataSet full code list reference

summary

The DataSet object is a key section of data access in the Microsoft .NET framework, which is a memory that can save tables, views, and relationships. This article describes how to use one or more database queries to populate

DataSet objects, and loading these data into

How to access these data after the DataSet object.

Back to top

Require the following list lists the recommended hardware, software, network infrastructure, and service packs required:

Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server or Windows NT 4.0 Server Microsoft SQL Server 7.0, Microsoft SQL Server 2000 or Pubs sample database installed Microsoft Data Engine Microsoft Visual Studio .NET This article assumes that you are familiar with the following topics:

Database term structured query language (SQL)

Back to top

Plip DataSet from

System.Data namespace uses multiple objects, you can connect to a database server, run the query, then put the result

DataSet objects.

DataSet is an object that is disconnected. Therefore, after loading the data, you will not use the connection to the database before you want to load more data or want to use the changes to the changes to the copy of these information.

To load data from the database

In DataSet, follow these steps:

Start Visual Studio .NET. Create a "Console Application" item in Visual C # .NET. Visual Studio .NET creates a static class and an empty main process. Make sure the item references the two namespaces of System and System.Data. Using the USING statement for System, System.Data, and System.Data.sqlclient namespace, this is not required to be declared in these namespaces in the following code. These statements must be used before any other statement. Using system;

Using system.data;

Using system.data.sqlclient The connection string in the code is connected to the SQL Server server on the local computer (computer running these code). You must modify the connection string accordingly based on the environment. Once you have created a SQLConnection object, call the OPEN method of the object to create a real database link. String sconnectionstring;

Sconnectionstring = "Password = mypassword; user ID = myUserid;"

"Initial Catalog = PUBS;"

"Data Source = (local)";

SqlConnection Objconn

= New SqlConnection (SCONNNEctionstring);

Objconn.Open (); create a DataAdapter object that represents the link between the database and the DataSet object. You can specify a command for retrieving data as part of the DataAdapter's constructor object. The following example uses a SQL statement to retrieve records from the AUTHORS table of the PUBS database. SqlDataAdapter Daauthors = New SqlDataAdapter ("Select * from authors", objconn; an instance of the DataSet object must be declared and you should provide a name for the entire DataSet before you can start loading any data. This name can contain several separate tables. DataSet DSPUBS = New Dataset ("Pubs"); SQLDataAdapter class provides two methods of Fill and Fillschema, which is critical to loading these data. Both methods can load information into the DataSet. Fill loads the data itself, while FillSchema loads all available metadata (such as column names, primary keys, and constraints) about a particular table. The correct way to handle data is to run FillSchema first, run Fill. For example: Daauthors.Fillschema (DSPUBS, Schematype.Source, "Authors");

Daauthors.Fill (DSPUBS, "Authors"); if you only use Fill, you can only load the basic metadata required to describe the column name and data type. The Fill method does not load primary key information. To change this default behavior, you can set the MissingsChemaAction property of the DataAdapter object to MissingsChemaAction.AddWithKey, which loads the primary key metadata with the default information. For example: daauthors.MissingSchemaAction = missingschemaAction.addwithkey;

Daauthors.Fill (DSPUBS, "Authors"); This data is now available as a separate DataTable object within the tablet's tables collection. If you specify a table name in the call to Fillschema and Fill, you can use this name to access the specific table you need. DataTable tblauthors;

TBLAUTHORS = DSPUBS.TABLES ["authors"]; You can use the for Each loop to sequentially through a DataTable Rows collection in all DATAROW objects. This will allow you to access each row of the table. You can access the column by name or by position index ("0" is the first column location). For example: Foreach (Datarow Drcurrent In TBLAUTHORS.ROWS)

{

Console.writeLine ("{0} {1}",

Drcurrent ["au_fname"]. TOSTRING (),

Drcurrent ["au_lname"]. TOSTRING ());

}

Console.readline (); save the project. On the Debug menu, click Start to run your project and make sure it can run normally.

Back to top

Complete code list

Using system;

Using system.data;

Using system.data.sqlclient;

Namespace populateDataSet

{

///

/// summary description for class1.

/// Class Class1

{

Static void main (string [] args)

{

String sconnectionstring;

Sconnectionstring = "Password = mypassword; user ID = myUserid;"

"Initial Catalog = PUBS;"

"Data Source = (local)";

SqlConnection Objconn

= New SqlConnection (SCONNNEctionstring);

Objconn.open ();

SqlDataAdapter Daauthors

= New SqldataAdapter ("Select * from authors", objconn);

DataSet DSPUBS = New Dataset ("Pubs");

Daauthors.Fillschema (DSPUBS, Schematype.Source, "Authors");

Daauthors.Fill (DSPUBS, "Authors");

DataTable tblauthors;

TBLAUTHORS = DSPUBS.TABLES ["authors"];

Foreach (Datarow Drcurrent In TBLAUTHORS.ROWS)

{

Console.writeLine ("{0} {1}",

Drcurrent ["au_fname"]. TOSTRING (),

Drcurrent ["au_lname"]. TOSTRING ());

}

Console.readline ();

}

}

}

Back to top

Refer to ADO.NET,

For more information on DataSet objects and SQL, please visit the following Microsoft Web site:

"DIVING INTO DATA Access" (in-depth understanding of data) (MSDN Voices column written by Dino Esposito) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndive/html /DATA06132002.ASP"ado.net for the ado programmer (ADO.NET) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotNet/html /adonetprogmsdn.aspmsdn online .Net Developer Center (MSDN Online .Net Developer Center) http://msdn.microsoft.com/net

Back to top

The information in this article applies to:

Microsoft ADO.NET (provided with .NET Frame) Microsoft Visual C # .NET (2002)

Recent Updated: 2002-7-9 (1.0) Keyword Kbhowto KbhowTomaster Kbsqlclient KbsystemData KB314145

转载请注明原文地址:https://www.9cbs.com/read-108914.html

New Post(0)