Visual c# 2005 ?? Visual c# .NET ?? ????? ?? ?? ??????? ?? ?? DataSet ???????? ?? ?????? ???? ?? ??? ????

???? ?????? ???? ??????
???? ID: 307587 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ??? ?? ??? ???? ???? ?? ?????? ???? (?? ???? ??????? ?? ??? ??) ??? DataSet ???? ?? ??? ???? ?????? ??, ?? ???? ?? ??????? ???? ????, ?? ???? ??? ??? ????? ?????? ???? ?? ??? ???? ??????? ??? ????? ?? ??? ?????

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

??????????

????? ???? ???????? ?????????, ??????????, ??????? ??????, ?? ?????? ??? ?? ?? ?? ???????? ?? ???? ??????? ????:
  • Microsoft Windows Server 2003, Microsoft Windows 2000Professional, Microsoft Windows 2000 ?????, Microsoft Windows 2000 AdvancedServer, ?? Microsoft Windows NT 4.0 ?????
  • Microsoft SQL Server 7.0, Microsoft SQL Server2000 ?? Microsoft ???? ????? (MSDE) ??????? ?? ??? PUBS ????? databaseinstalled
  • Microsoft Visual Studio 2005 ?? Microsoft Visual Studio.NET
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • ??????? ????????
  • ?????? ?????? ???? (SQL)

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

?? ?????? ????????? ???? ?? ?? ???? ??????? ??? ???? ?????? ???? ?? ??? DataSet ???????? ?? ????? ???? ????? ?? ?? ?? SqlCommand ???????? ???????? ????, ??????, ?? ???? ???? ??????? ??? ???? ?? ????? ?? ??? ?? ?? ?? ????? ?? ???? ???, ?? ??? ???? ?????????? ???

?? ???? ?? ????? ??? ??? ???? ?? ???, Microsoft ?????? ??? ??? ???? ????? ?? ??? ????? ???? ?????? ?? ????? ????:
314145 ????? c# .NET ?? ????? ?? ?? ??????? ?? ?? DataSet ???????? ?? ????????? ???? ?? ??? ????
??? ?????? ??? ????? ??? 314145 ?? ??? DataSet??? ???? ??????? ?? ???? ???????????? ???? ?? ??? ???? ???? ?? ???? DataSet ???-??? ??????? ?? ????? ?? ?? ????? ???

??????? ??? ???? ?? ???, ?? ???? ?? ??????? ?? ???? ???? DataSet ?? ?????????? ?? ????? ?????? DataSet ???????? ??????? ?? ??????? ?? ?? ???? ?? ??? ??????-??? ??? ???? ?? ?? ???? ???? DataSet ???????? ????????, ????? ?? ?????? ?? ?????? ???? ???

??? DataSet ??????, ?? ?? ?????? ???? ??????? ?? ????? ?? ???, ????? ????? ?? ???? ????:
  1. Visual Studio 2005 ?? Visual Studio .NET ??????? ?????
  2. ??? ??? ????? ????????? Visual C# ??? ?????? VisualStudio ?? ????? ???? ?? ?? ???? Main() ????????? ???????? ?????? ????? ???
  3. ????????? ???? ?? ????????? ?????? ?? System.Data ???????? ?? ?????? ????? ???? ??? ?? ????? ????????, System.Data?? System.Data.SqlClient ???????? ?? ?? ????? ???? ???? ?? ??? ??? ???? ??? ??? ?? ???????? declarationsfrom ????? ???? ?? ??? ???????? ???? ??? ?? ???? ?? ???? ????? ?? ????? priorto ?? ????? ???? ??????
    using System;
    using System.Data;
    using System.Data.SqlClient;
    					
  4. ?? ???? ?? ??????? ?? ???? ??? ?? ???????? backto ??????? ?? ????? ???? ?? ????, ???? DataSet??? ??????? ??? ???? ?????? ??????? ????????? ?? ???, ?????314145. ??? ???? ???????? ????? ??? ?? stepis ??? ?????????? ?? ???? ?? ????

    ??????? ???????? ??? followingcode ??????? ?? SQL ????? ?? ??? ??????? ???????? (?? ???? ??? ?? ??? thecomputer ??) ????? ?? ?? ???? ?? ??? ?????????, ?? ??????? ????? ??, andthen ?? ???? ??????? ?? ?????, ?? DataSet ???? ?? ??? ???? ?? ??? ????? ???? ???? ???
    ??? ?? ?? ??? ?? ????? ?? ???? ???? ??? ??? ?? ??? <username>?????????? ID ?? ???????<strongpassword> ????????? ???? ?????? ??? ????????? ???? ?? thatUser ID thedatabase ?? ?? ???????? ???? ?? ??? ???? ?????? ??.</strongpassword> </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");
    					
  5. ?? ?? ???? ??? ??, ???? ??? ??????? ?? ???? ???? ???? aremany ????? ?? ???? ?????? (?? ???????) ??????? ?? ??? ????? ?? ??? stepprocedure ?? ????? ???? ??:
    • ??? ??? DataRow ???????? DataTable?? ??????? ?????
    • DataRow ?????? ??? ?????? ?? ??? ??? ??? ?????
    • ?? ??? ???????? ??? ?????? ???? 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  
    					
  6. ?????? ????????? ?? ??????? ???? ?? ???, ??????? DataRow ???????? ??????? ????, ?? ?? ?? ??? ?? ??? ?? ?? ???? ????? ??????? ?? ???? ???? ?? ??????? ?? theschema ?????? ?? ??? ?? (??? FillSchema ???? ?? ??? ??? 4) ???? ?? ??? ??? ??????, Youmust ???? ??????? ????? ??? ?????? ???, ?????? ???????? ????? whichcolumn ??, ?? ?????? ?????? ?? ??? ???? ?????? ?? ????? ???

    ??? ???? DataRow ???????? ??? ??????? ??? ?? ??? ???? ???????? ????? (?? ????? ???, au_id) ??? ???? ??? ?? ?? DataRow?? ???? ?? ???, ?? ??????? ?? ??????? ?? ???? ???? ?? themodifications BeginEdit ?? EndEdit??? ??? ???? ?? ??? ???? ??, ????? ?? ????? DataSet ???? ?? ??? ??, ?? EndEdit ??? ???? ??, ?? ???? ??????? ???? ?? ?? ??? ???? ?? ??? DataSet ???? ???? ?????? ??? ?? ??? ????? ??? ???????:
    //*****************
    // 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  
    					
  7. ??? ?? ?????????? ?? ??? ??? ??????? ?????? ???? ?? ??? DataSet ??? ?????? ???? DataAdapter ???????? ?? ??? ?????

    ???????, ???? ???? ?? ?? ???????? ??? ?? ???? ???, ???? InsertCommand, UpdateCommand, ?? DeleteCommand ??? DataAdapter ???????? ??? ???? ?????? ??? ?? ?? ???? ??? ???????? ??? ?? SQL ????? ?? ?? threeproperties ?? ??? ???? SqlCommand ???????? ?? ?????????, ????? ?? ?? Visual Studio .NET generatethese ??? ???? ???? ?? ??? ???????? ??? ?? ????? ?? ???? ????

    ?? ?????? ??? ?????? commandswhen ??????? ???? ?? ???, ?? SqlCommandBuilder ???????? ?? ??????? ????? ?? ???????? ??? ?? ????? ???? DataAdapter . ?? ??????, ?? isillustrated ?? ???? ???? ?? ???, ??? ????? ?? ????? ???? ?? ??? ????? ???, ?? ?? ???? ?????? ?? ??? ???????? ????? informationavailable ???? ?????? ??? ???????? ????? ??????? ?? ????? ??????? ???? ?? ???, ??? FillSchema, ?? ??? ?? MissingSchemaAction ??? ???? DataAdapter ???? ?? ??? AddWithKey??? ????, ?? ???????? ??? ?? ???? ??? ??? ???????? ????? ??? ????? ?????? ??? ?? ??? thefollowing ??? ???????:
    //*****************
    // 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
    					
  8. ???? ?????? ???? ??? ?? ????? ?? ???, ????? ???? DataRow ???????? ?? ????? ????? ??? ???? ?? ?????? ?????? ?? ???????, ??????? , ?? RemoveAt, ?? ?????? ?????, ????? ???? ???? ????? rowfrom ?????? ??????? ?? ??? ??? ?? ??? ??? ???? ????? ???? ???? ????? ?? ???? ????? ??????? ?? ??? ????? ??? Thefollowing ??? ???? ?? ??? ????? ???????? ???? ?? ??? SQL SERVER ??? ???????:
    //*****************
    //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 
    					
  9. ???????? ??????? thatyou ???? ????? ?? ??????? ?? ??? SQL ????? ?? ??? ?????? DELETE ??? ?? ??? ????? ??? ???????:
    //*****************
    // CLEAN UP SQL SERVER
    daAuthors.Update(dsPubs, "Authors");
    Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
    Console.ReadLine();
    					
  10. ???? ????????? ?? ???????
  11. ????????? ?? ????? ?? ??? ??????? ???????? ???? ???? ??, ????? ????? ?? ??? ?? ?????? ?? ????? ?? currentstate ??? ?????? ???? ?? ??? ??? ???? ?? ??????? ???? ?? ?????? ??? ?? ???? ????? ????? ????? ??, ??????

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

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 ?? ????? ???? ?? ???? ??? ???? ??????? ?? ???, DataSet ????????, ?? SQL, ????? Microsoft ??? ???? ?? ????:
???? ????? (?? MSDN Voices ?????) ??? diving
http://msdn2.microsoft.com/en-us/library/ms810288.aspx
ADO ?????????? ?? ??? ADO.NET
http://msdn.microsoft.com/en-us/library/ms973217.aspx
MSDN ?????? .NET ?????? ??????
http://msdn2.microsoft.com/en-us/netframework/default.aspx

???

???? ID: 307587 - ????? ???????: 29 ??????? 2013 - ??????: 3.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

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

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com