???? ID: 314145 - ????? ???????: 04 ?????? 2010 - ??????: 2.0

????? C# .NET ?? ????? ?? ?? ??????? ?? ?? DataSet ???????? ?? ????????? ???? ????

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

DataSet????????, ???? ????? ??? ??? Microsoft .NET Framework ???? ?? ????? ?? ???-?????? ???????? ???? ??????, ????? ?? ????? ????? ?? ???? ???? ????????? ???? ?? ?? ???? ?? ???? ???? ?? ??? ???DataSet?? ?? ???? ??????? ?????? ?? ??? ??? ??? ?? ?? ???? ?? ??????? ?? ??? ???? ?? ?????? ?? ??? ????????DataSet???????? ???

??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ??????, ?? ???? ???? ???:
  • Microsoft Windows 2000 Professional, Windows 2000 ?????, Windows 2000 ????? ????? ?? Windows NT 4.0 ?????
  • Microsoft SQL Server 7.0, Microsoft SQL Server 2000 ?? Microsoft ??????? Pubs ????? ??????? ?? ???? ?????
  • Microsoft Visual Studio .NET
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • ?????? ?? ???????
  • ?????? ?????? ???? (SQL)

??? DataSet ???? ????

??????? ?????? ?? ?????????? ?? within ?? ????? ????System.Datanamespace, you can connect to a database server, run a query, and have the results placed into aDataSet???????? ??? TheDataSetis a disconnected object. Therefore, after the data is loaded, the connection to the database is no longer used until you want to load more data or update the server with the changes you have made to your in-memory copy of the information.

To load data from a database into aDataSet?? ????? ?? ???? ????::
  1. Visual Studio .NET ?? ??????? ?????
  2. Create a new Console Application project in Visual C# .NET. Visual Studio .NET creates a Static Class for you, along with an empty??????? ??? 1 ?? ??? ?? ????..
  3. ????????? ???? ?? ????????? ?? ?????? ?????????, ??System.Data?????????
  4. ??????? ????? ?????? ???????? ?????????,System.Data, ??System.Data.SqlClient???????? ???? ?? ??? ??? ???? ??? ??? ?? ???????? ?? declarations ????? ????? ?? ??? ?????? ???? ???? ?? ???? ?? ???? declarations ???? ?? ???? ?? ????? ?? ????? ???? ?????? ???
    using System;
    using System.Data;
    using System.Data.SqlClient;
    					
  5. ???? ??? ?? ??? ??????? ?? ???? ??????? ???? ?? ???DataSet??? ??????? ???????, ?? ???????? ?? ?? ??????? ???? ?? ??? ?? ??System.Data.SqlClient.SqlCommand???????? ?? ?? ??????? ????????? ??????? ???????? ?? ???? ???? ?? ??? ??? ??? ??????? ???????? (???????? ???? ??? ??) ?? ????? ???? SQL Server ????? ?? ?????? ???? ??? ?? ???? ?????? ?? ??? ??????? ?? ??? ??? ?? ??????? ???????? ?? ??????? ???? ?????? ??? ???? ???SqlConnection???????? ????? ??? ??, ???????????????? ??????? ???? ??????? ???? ?? ??? ?? ???????? ?? ?????? ???
    string sConnectionString;
    sConnectionString = "Password=myPassword;User ID=myUserID;"
      					   + "Initial Catalog=pubs;"
    						+ "Data Source=(local)";
    SqlConnection objConn
    	= new SqlConnection(sConnectionString);
    objConn.Open();
    					
  6. ????? ???DataAdapterobject, which represents the link between the database and yourDataSet???????? ??? You can specify SQL or another type of command that is used to retrieve data as part of the constructor object of theDataAdapter. This sample uses a SQL statement that retrieves records from the Authors table in the Pubs database.
    SqlDataAdapter daAuthors 
    	= new SqlDataAdapter("Select * From Authors", objConn);
    					
  7. You must declare and create an instance of aDataSetobject, at which time you can supply a name for the entireDataSetbefore you can start to load any data. The name may contain several distinct tables.
    DataSet dsPubs = new DataSet("Pubs");
    					
  8. TheSqlDataAdapterclass provides two methods,???, ??FillSchema, that are crucial to loading this data. Both of these methods load information into aDataSet.???loads the data itself, andFillSchemaloads all of the available metadata about a particular table (such as column names, primary keys, and constraints). A good way to handle the data loading is to runFillSchema?? ???? ??????. ?????? ?? ???::
    daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors");
    daAuthors.Fill(dsPubs,"Authors");
    						
    If you only use???, you can only load the basic metadata that is required to describe the column names and data types. The???method does not load primary key information. To change this default behavior, you can set theMissingSchemaAction?? ???DataAdapterobject toMissingSchemaAction.AddWithKey, which loads the primary key metadata along with the default information. ?????? ?? ???::
    daAuthors.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    daAuthors.Fill(dsPubs,"Authors");
    					
  9. ???? ?? ???? ??????? ?? ??? ??? ?????? ??DataTable??? ?????????????????? ??????DataSet. ??? ??? ???? ??? ??? ?????? ??? ?????????FillSchema, ?????, ?????? ????? ?????? ??? ?????? ???? ?? ??? ??? ?? ????? ?? ???? ????
    DataTable tblAuthors;
    tblAuthors = dsPubs.Tables["Authors"];
    					
  10. ????? ?? ???? ??? ??? For Each ??? ?? ??? ??? ??? ?? ?????? ??DataRow??? ??????????????????? ?????? ????DataTable. ?? ?????? ?? ???????? ?????? ?? ??? ?? ????? ???? ??? ?? ?? ????? ???? ????? ??? ?? ?? positional ??????????? (?? '0' ???? ????? ?? ?????? ???? ?? ??? ??)? ?????? ?? ???::
    foreach (DataRow drCurrent in tblAuthors.Rows)
    {
    	Console.WriteLine("{0} {1}",
    		drCurrent["au_fname"].ToString(),
    		drCurrent["au_lname"].ToString());
    }
    Console.ReadLine();
    					
  11. ???? ????????? ?? ??????? ????? ???????? ???????? ??,??????????? ????????? ?? ????? ?? ????????? ???? ?? ?? ??? ?????

??? ??????? ?????

using System;
using System.Data;
using System.Data.SqlClient;

namespace PopulateDataSet
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	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(sConnectionString);
			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();
		}
	}
}
				

??????

ADO.NET, ?? ???? ??? ???? ??????? ?? ???DataSet???????? ?? SQL ?? ???, ????? Microsoft ??? ???? ?? ??? ??????:
"???? ????? ??? diving" (??MSDN VoicesDino Esposito ?????? ?????)
HTTP://msdn2.Microsoft.com/en-us/library/ms810293.aspx (http://msdn2.microsoft.com/en-us/library/ms810293.aspx)

ADO ?????????? ?? ??? ADO.NET
HTTP://msdn2.Microsoft.com/en-us/library/ms973217.aspx (http://msdn2.microsoft.com/en-us/library/ms973217.aspx)

MSDN ?????? .NET ?????? ??????
HTTP://MSDN.Microsoft.com/en-us/netframework/default.aspx (http://msdn.microsoft.com/en-us/netframework/default.aspx)

???? ???? ???? ??:
  • Microsoft ADO.NET 1.1
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
??????: 
kbhowtomaster kbsqlclient kbsystemdata kbmt KB314145 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:314145  (http://support.microsoft.com/kb/314145/en-us/ )