?? ??? ?? ??? ???? ???? ????? ???? ???? ?? ??? ???
DataSet?????? ???? (?? ???? ??????? ?? ??? ??), ?? ???? ?? ??????? ???? ?? ??? ???? ????, ?? ???? ??? ??? ????? ?????? ???? ?? ??? ??????? ??? ????? ?? ??? ???? ?????
DataSet????????, ???? ????? ??? ??? Microsoft .NET Framework ???? ?? ????? ?? ???-?????? ???????? ???? ??????, ????? ?? ????? ????? ?? ???? ????
??????????
????? ???? outlines ???????? ?????????, ??????????, ??????? ??????, ?? ???? ???? ???:
- Microsoft Windows Server 2003, Microsoft Windows 2000 Professional, Microsoft Windows 2000 ?????, Microsoft Windows 2000 ????? ????? ?? Microsoft Windows NT 4.0 ?????
- Microsoft SQL Server 7.0, Microsoft SQL Server 2000 ?? Microsoft ???? ????? (MSDE) ??????? PUBS ?? ??? sample ??????? ???????
- Microsoft Visual Studio 2005 ?? Microsoft Visual Studio .NET
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
- ?????? ?? ???????
- ?????? ?????? ???? (SQL)
??? DataSet ???????? ??? ?? ???? ??????? ?? ?????? ???? ?? ??? ???? ????
????????? ???? ?? ?? ??? ?? ?? ????? ???? ????
DataSet???????? ???? ??????? ??? ???? ?????? ???? ?? ??? ??? ??? ???? ?? ?? ?? ???? ??? ?? ?? ????? ???? ?? ??? ?????????? ?? ??
SqlCommand???????? ???????? ????, ??????, ?? ???? ???? ??????? ??? ???? ?? ????? ?? ??? ???
??? ?? ??? ?? ???? ?? ????? ???, Microsoft ???????? ??? ???? ????? ?? ??? ????? ???? ?????? ?? ????? ????:
314145
(http://support.microsoft.com/kb/314145/EN-US/
)
????? C# .NET ?? ????? ?? ?? ??????? ?? ?? DataSet ???????? ?? ????????? ???? ????
??? ?????? ??? ????? ???
314145
(http://support.microsoft.com/kb/314145/EN-US/
)
??? ?? ???? ??????? ?? ???? ?? ??????? ???? ?? ??? ???? ????? ??
DataSet, ?? ????
DataSet??????? ?? distinct ?? ??? ???
???? ???
DataSet???, ???? ?? ??????? ?? ???? ??? The
DataSetwill track these changes. The
DataSetobject can be considered an in-memory cache of data that is
retrieved from a database. The
DataSetobject consists of a collection of tables, relationships, and
constraints.
To update a
DataSetand send those updates back to the database, follow these steps:
- Visual Studio 2005 ?? Visual Studio .NET ??????? ?????
- Create a new Console Application in Visual C#. Visual
Studio creates a Static Class by default and an emptyMain()?? ??? 1 ?? ??? ?? ????..
- ????????? ???? ?? ????????? ?? ??? ??? ?????? ?????????, ??System.Datanamespaces. ??????? ????? ?????? ???????? ?????????,System.Data, ??System.Data.SqlClientnamespaces so that you are not required to qualify declarations
from these namespaces later in your code. You must use these statements prior
to any other declarations.
using System;
using System.Data;
using System.Data.SqlClient;
- ???? ???? ?? ?? ???? ?? ??????? ?? ???? ??? ?? ???? ??????? ???????? ????? ????, ?? ??? ??????? ??? ???? ????DataSet. ??????? ????????? ?? ??? ?????314145
(http://support.microsoft.com/kb/314145/EN-US/
)
. ?????????? ?? ????, ?? ??? ?? ??? ??? ??? ?? ???? ???????? ????? ????
??????? ???????? ??? ????? ??? ????? ??????? ???????? (?? ???????? ?????? ??? ??) ?? ????? ???? SQL ????? ?? ???, ?????? ?? ??? ??? ??????? ????? ??, ?? ???? ??? ?? ???? ??????? ?? ???? ??, ?? ???? ?? ??? ????? ???? ???? ??DataSet?????
???:?? ????????? ???? ?????? ?????????? ID <username>?? <strong password="">??????? ??? ??? ?? ??? ?? ??? ?? ????? ?? ????? ????????? ???? ?? ?????????? ID ??? ???????. ?? ?? ???????? ???? ?? ??? ??????? ?????????</strong></username>
string sConnectionString;
// Modify the following string to correctly connect to your SQL Server.
sConnectionString = "Password=<strong password>;User ID=<username>;"
+ "Initial Catalog=pubs;"
+ "Data Source=(local)";
SqlConnection objConn
= new SqlConnection(sConnectionString);
objConn.Open();
// Create an instance of a DataAdapter.
SqlDataAdapter daAuthors
= new SqlDataAdapter("Select * From Authors", objConn);
// Create an instance of a DataSet, and retrieve data from the Authors table.
DataSet dsPubs = new DataSet("Pubs");
daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors");
daAuthors.Fill(dsPubs,"Authors");
- ?? ?? ???? ??? ??, ???? ??? ??????? ?? ???? ???? ???? ?????? (?? ???????) ??? ?????? ?? ??? ?? ????? ???? ?? ??? ????? ?? ??? ??? ????????? ?? ????? ???? ??:
- ??? ??? ??????? ????DataRow?? ????????DataTable.
- ??? ????DataRow?????? ??? ?? ??? ??? ?????? ???
- ?? ?? ???????? ??? ???add?? ??? ????DataTable.Rows?????? ???
??? 4 ??? ??? ?? ??? ?? ????? ??? ???????:
//****************
// BEGIN ADD CODE
// Create a new instance of a DataTable.
DataTable tblAuthors;
tblAuthors = dsPubs.Tables["Authors"];
DataRow drCurrent;
// Obtain a new DataRow object from the DataTable.
drCurrent = tblAuthors.NewRow();
// Set the DataRow field values as necessary.
drCurrent["au_id"] = "993-21-3427";
drCurrent["au_fname"] = "George";
drCurrent["au_lname"] = "Johnson";
drCurrent["phone"] = "800 226-0752";
drCurrent["address"] = "1956 Arlington Pl.";
drCurrent["city"] = "Winnipeg";
drCurrent["state"] = "MB";
drCurrent["contract"] = 1;
// Pass that new object into the Add method of the DataTable.
tblAuthors.Rows.Add(drCurrent);
Console.WriteLine("Add was successful, Click any key to continue!!");
Console.ReadLine();
// END ADD CODE
- ?????? ????????? ?? ??????? ???? ?? ??? ??????? ???????DataRowobject, and then provide new values for one or more columns. You
must first find the correct row, which is much easier because you loaded the
schema of the table as well as the data (the call toFillSchemain step 4). With the schema in place, the table knows which
column is its primary key, and the???????? ??? ?????????????collection is available.
The??????method returns theDataRowobject with a specific value in its primary key (in this case,
au_id). After you have thatDataRow, you can modify the columns. You do not have to wrap the
modifications inBeginEdit, ??EndEdit, ????? ?? ????? simplifies ??DataSet???? ???? ?? ?? ?? ?????? ???? ??DataSet???? ?? ??? ??????? ???? ???? ?? ??? ??EndEdit??? ???? ??? ADD ??? ?? ??? ?? ????? ??? ???????:
//*****************
// BEGIN EDIT CODE
drCurrent = tblAuthors.Rows.Find("213-46-8915");
drCurrent.BeginEdit();
drCurrent["phone"] = "342" + drCurrent["phone"].ToString().Substring(3);
drCurrent.EndEdit();
Console.WriteLine("Record edited successfully, Click any key to continue!!");
Console.ReadLine();
// END EDIT CODE
- ??? ?? ?????????? ?? ??? ??? ??????? ?? ?????? ???? ?? ??? ??? ????DataSet??????????? ??? ????DataAdapter???????? ???
???????, before ?? ??? ?? ??????????, ??? ???? ????InsertCommand,UpdateCommand, ??DeleteCommand?? ???DataAdapter???????? ??? ?? ?? ???? ??? ???????? ??? ?? SQL ????? ?? ?? ??? ??? ?? ??? ???? ?????????SqlCommand????????, ????? ?? ?? ????? ?? ???? Visual Studio .NET ?? ??? ?????? ?? ????: ????? ???? ?? ????
?? ?? ?????? ?????? ????????? ?? ?????, ?? ??? ???? ?? ??????? ????? ????SqlCommandBuilder???????? ?? ?? ????? ????DataAdapter??? constructor. ??? ?? ?? ???? ?? ?????, ?? ???? ???? ?? ??? ??? ????? ??? illustrated ??, ?? ???? ????? ???, ?? ?? ???? ?????? ?? ??? ???????? ????? ??????? ?????? ???? ?????? ???????? ????? ??????? ?? ?????, ???FillSchema, ?? ???? ??? ??? ????MissingSchemaAction?? ??? ????DataAdapter???? ?? ???AddWithKey, ?? ???????? ??? ?? ???? ??? ??? ???????? ????? ?? ??? ????? ?????? ??? ?? ??? ?? ????? ??? ???????:
//*****************
// BEGIN SEND CHANGES TO SQL SERVER
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(daAuthors);
daAuthors.Update(dsPubs, "Authors");
Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
Console.ReadLine();
// END SEND CHANGES TO SQL SERVER
- ???? ?????? ???? ??? ?? ????? ?? ??? ????? ??????????? ??? ????DataRow???????? ??? ?? ?????????????????? ?? ?? ??????? ????????, ??RemoveAt, ?? ?????? ?????? ?? ????? ?? ???? ????? ?? ???? ?????? ?? ??????? ?????? ???? ?? ???? ????????????? ????? ??????? ??? ???? ???? ????? ??? ????? ???????? TO SQL ????? ??? ?? ??? ?? ????? ??? ???????:
//*****************
//BEGIN DELETE CODE
drCurrent = tblAuthors.Rows.Find("993-21-3427");
drCurrent.Delete();
Console.WriteLine("Record deleted successfully, Click any key to continue!!");
Console.ReadLine();
//END DELETE CODE
- ???????? ??????? ?? ???? ???? ???? ????? ?? ??????? ?? ??? SQL ????? ?? ??? ?????? DELETE ??? ?? ??? ?? ????? ??? ???????:
//*****************
// CLEAN UP SQL SERVER
daAuthors.Update(dsPubs, "Authors");
Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
Console.ReadLine();
- ???? ????????? ?? ???????
- ????? ???????? ???????? ??,???????????????? ?? ?????? ????? ?? ???? ????? ????? ????? ??, ?? ??? ?? ?????? ?? ????? ?? ?? ???? ??? progresses ?? ??? ??? ???? ?? ??????? ?????? ?? ??????? ???? ?? ?????? ????
??? ??????? ?????
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;
// Modify the following string to correctly connect to your SQL Server.
sConnectionString = "Password=;User ID=sa;"
+ "Initial Catalog=pubs;"
+ "Data Source=(local)";
SqlConnection objConn
= new SqlConnection(sConnectionString);
objConn.Open();
// Create an instance of a DataAdapter.
SqlDataAdapter daAuthors
= new SqlDataAdapter("Select * From Authors", objConn);
// Create an instance of a DataSet, and retrieve
// data from the Authors table.
DataSet dsPubs = new DataSet("Pubs");
daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors");
daAuthors.Fill(dsPubs,"Authors");
//****************
// BEGIN ADD CODE
// Create a new instance of a DataTable.
DataTable tblAuthors;
tblAuthors = dsPubs.Tables["Authors"];
DataRow drCurrent;
// Obtain a new DataRow object from the DataTable.
drCurrent = tblAuthors.NewRow();
// Set the DataRow field values as necessary.
drCurrent["au_id"] = "993-21-3427";
drCurrent["au_fname"] = "George";
drCurrent["au_lname"] = "Johnson";
drCurrent["phone"] = "800 226-0752";
drCurrent["address"] = "1956 Arlington Pl.";
drCurrent["city"] = "Winnipeg";
drCurrent["state"] = "MB";
drCurrent["contract"] = 1;
// Pass that new object into the Add method of the DataTable.
tblAuthors.Rows.Add(drCurrent);
Console.WriteLine("Add was successful, Click any key to continue!!");
Console.ReadLine();
// END ADD CODE
//*****************
// BEGIN EDIT CODE
drCurrent = tblAuthors.Rows.Find("213-46-8915");
drCurrent.BeginEdit();
drCurrent["phone"] = "342" + drCurrent["phone"].ToString().Substring(3);
drCurrent.EndEdit();
Console.WriteLine("Record edited successfully, Click any key to continue!!");
Console.ReadLine();
// END EDIT CODE
//*****************
// BEGIN SEND CHANGES TO SQL SERVER
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(daAuthors);
daAuthors.Update(dsPubs, "Authors");
Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
Console.ReadLine();
// END SEND CHANGES TO SQL SERVER
//*****************
//BEGIN DELETE CODE
drCurrent = tblAuthors.Rows.Find("993-21-3427");
drCurrent.Delete();
Console.WriteLine("SRecord deleted successfully, Click any key to continue!!");
Console.ReadLine();
//END DELETE CODE
//*****************
// CLEAN UP SQL SERVER
daAuthors.Update(dsPubs, "Authors");
Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
Console.ReadLine();
}
}
}
ADO.NET, ?? ????? ???? ?? ???? ??? ???? ??????? ?? ???
DataSetobjects, and SQL, visit the following Microsoft Web sites:
???? ID: 307587 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
- Microsoft Visual C# 2005
- Microsoft Visual C# .NET 2003 Standard Edition
- Microsoft Visual C# .NET 2002 Standard Edition
| kbhowtomaster kbsqlclient kbsystemdata kbmt KB307587 KbMthi |
???? ?????? ??????????????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:
307587
(http://support.microsoft.com/kb/307587/en-us/
)