????? ????? ????? ?????? ?? ???? DataSet ???????? .NET Visual C++ ?? Visual C++ 2005

?????? ????????? ?????? ?????????
???? ???????: 815660 - ??? ???????? ???? ????? ????? ??? ???????.
????? ???? | ?? ????

?? ??? ??????

??????

???? ??? ??????? ???? ????? ??? ????? ?????? ??? DataSet ???? ????? ??? ???????? (???? ??? ?????? ?? ????? ??????) ? ????? ????? ???????? ?????? ?? ??????? ??? ????? ???????? ?????? ?????? ??????.

?????? DataSet ??? ????? ?? ?????? ??? ???????? ?? Microsoft ".NET Framework?" ?? ?????? ?? ??????? ???? ???? ???????? ??????? ???? ????? ????????.

???????

???? ??????? ??????? ??????? ?????? ??? ????? ?????? ???????? ?????? ? ??? ??????? ???? ???????:
  • ??? ????????? ???????: Microsoft Windows 2000 Professional ?? ???? ??????? Microsoft Windows 2000 Server Microsoft Windows 2000 Advanced Server ?? Microsoft Windows NT 4.0 Server
  • ?? ??? ???: ????? Microsoft SQL Server ??????? 7.0 ? Microsoft SQL Server 2000 ?? ???? ???????? ?? Microsoft "(MSDE) ?? ????? ????? ?????? pubs (????????) ???????? ???????? ???????? ???? ???????? ????? ?????? ?????.
  • Microsoft Visual Studio .NET ?? Microsoft Visual Studio 2005
????? ??? ??????? ??? ??????? ??? ???????? ???????:
  • ??????? ????? ????????
  • ??? ????????? ???????? (SQL)


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

???? ??? ????? ????? ??????? ?????? DataSet ?????? ???????? ?? ????? ??????.

??? ????? ????? ??????? ???? SqlCommand ??? ????? ???????? ??? ???????? ?? ????? ?????? ???????.

??? ????? DataSet ????? ????? ????????. ??????? DataSet ??? ?????????. ???? ?????? ?????? DataSet ?? ????? ??????? ???? ?? ???????? ???? ??? ????????? ?? ????? ??????. ????? ???? DataSet ?? ?????? ?? ??????? ?????? ?????.

????? DataSet ????? ??? ????????? ??? ???? ??? ????? ???????? ???? ??????? ???????:
  1. ???? Microsoft Visual Studio .NET ?? Microsoft Visual Studio 2005.
  2. ????? ???? ??????? C++ ????? ?? Visual C++ .NET. ???? Visual Studio ?????? ???? main(). ????? ??? ??????? ?????? ?? ?????? ?? ???? main().
  3. ?????? ???? ??? ?????? ????? "????" ? "System.Data ?????? ???????? ???????? ???????
    #using <System.Dll>
    #using <System.Data.Dll>
    #using <System.Xml.Dll>
  4. ?????? ????? USING ??? ?????? ????? ????System.Data ? System.Data.SqlClient ???? ??? ?????? ????? ????????? ?? ?????? ??????? ??? ???? ??? ?? ????????? ???????? ?????? ??. ??? ??????? ??? ???????? ??? ?? ????????? ??????.
    using namespace System;
    using namespace System::Data;
    using namespace System::Xml;
    using namespace System::Data::SqlClient;
    
  5. ??? ?? ????? ????? ???????? ?????? ????? ????????? ??? ???? ??? ????? ???????? "? ??? ????? ????????? ??? DataSet.

    ???? ????? ??????? ?? ????????? ???????? ??????? ??? ???? SQL ???????? ??? ????????? ?????? (?? ????????? ??? ??? ????? ????????? ????????). ??????? ??? ??????? ????????? ?????? ??.

    ?????? ? ??? ????? ????? ??? ?? ???? ?????? ?????, ???? ??????? ?????? DataSet ?? ????????.
    SqlConnection  *objConn;
    String *sConnectionString;
    sConnectionString = "Password=StrongPassword;User ID=UserName;Initial Catalog=pubs;Data Source=(local)";
    
    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");
    
    ?????? ??? ???? ????? ??? ????? ????? ?????? ????? ?????? ??????? (/ clr:oldSyntax) ?? Visual C++ 2005 ????? ????? ????????? ???????? ?????? ?????. ?????? ??????? ??? ??? ??????? ??? ?????? ??????? ??????? ?????? ?? Visual C++ 2005 ???? ??????? ???????:
    1. ???? ??? Project ?? ???? ??? <ProjectName> ?????.

      ??????<ProjectName> ?????? ?????? ??? ???????.
    2. ?? ?????? ????? ????? ?? ???? ??? ???.
    3. ???? ?????? ??? ????? ????? ?????? ??? ? ?????? ??????? (/ clr:oldSyntax) ?? ??????? ??????? ????? "??? ????? ????? ??????" ?? ????? ?????? ???? ??? ????? ?? ???? ??? ?????.
    ?????? ??? ???? ?? ????????? ??? ??? ????? ????? ?????? ????? ?????? ?????? ??????? ??????? ?? ?????? ???? Microsoft ?????? ??? ?????:
    / ??? (??????? ??? ????? ????? ??????)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx
    ??? ??????? ????? ??????? ???????.
  6. ????? ??? ?? ??? ????? ???????? ????? ??????. ???? ?????? ?? ????? ?????? ?? (?? ???). ?????? ????? ????????? ???????? ??? ????? ???? ?????:
    1. ?????? ??? ???? DataRow ???? ?? DataTable.
    2. ????? ??? ????? DataRow ??? ???????.
    3. ????? ??? ?????? ?????? ?? ??????? Add ???????? DataTable.Rows.

    ?? ???? ???????? ???????? ??????? ??? ???????? ???????? ?? ?????? 5:
    // BEGIN ADD CODE 
    // Create a new instance of a DataTable.
    DataTable* tblAuthors = dsPubs->Tables->Item["Authors"];
    
    
    // Obtain a new DataRow object from the DataTable.
    DataRow* drCurrent = tblAuthors->NewRow();
    
    // Set the DataRow field values as necessary.
    drCurrent->set_Item("au_id",new String("993-21-3427"));
    
    drCurrent->set_Item("au_fname",new String("George"));
    drCurrent->set_Item("au_lname",new String("Johnson"));
    drCurrent->set_Item("phone",new String("800 226-0752"));
    drCurrent->set_Item("address", new String ("1956 Arlington Pl."));
    drCurrent->set_Item("city", new String("Winnipeg"));
    drCurrent->set_Item("state", new String("MB"));
    drCurrent->set_Item("contract",__box(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  
    
  7. ?????? ?????? ???????? ?????? ??? ???? DataRow ??????? ?? ?? ?????? ????? ??????? ???? ?? ???? ?? ???????. ??? ?? ??? ????? ???? ?????? ????? ???? ???? ????? ???? ?????? ???????? ??? ???????? (??????? FillSchema ?? ?????? 5). ?? ?????? ?? ???? ???? ?????? ???? ?????? ?? ??????? ??????? ????? ?? ??? ????? ????? ??? ?? ?????? ??????.

    ???? ??????? Find ?????? DataRow ?? ???? ????? ?? ?????? ??????? (?? ??? ??????? au_id). ??? ?? ???? DataRow ????? ????? ???????. ?? ??? ???? ?????? ????????? ?? BeginEdit ? EndEdit ???? ????? ??? ????? ????? ??? ?????? ????? DataSet ????? ?????? ?????? ?? ?????? ?? ????? ????? ?? ?? ??? ????? ??? ??????? EndEditDataSet.

    ?? ???? ???????? ???????? ??????? ??? ???????? ???????? ?? ?????? 6:
    // BEGIN EDIT CODE 
    
    drCurrent = tblAuthors->Rows->Find(new String("213-46-8915"));
    drCurrent->BeginEdit();
    
    drCurrent->set_Item("phone",String::Concat(S"342",(static_cast<String *>(drCurrent->Item["phone"]))->Substring(3)));
    drCurrent->EndEdit ();
    Console::WriteLine("Record edited successfully, Click any key to continue!!");
    Console::ReadLine();
    
    // END EDIT CODE
    
  8. ?????? ????? ???????? ??????? ?? ???? ??? ????????? ????? DataSet ??? ????? ????? ?????? DataAdapter.

    ??? ???? ??? ??????? ????? ??? ???? ????? ????? ?????? DataAdapterInsertCommand ? UpdateCommandDeleteCommand. ????? ?????? ????? SQL ?????? ??? ??? ??????? ????? ?? ?????? SqlCommand ??????? ??? ????? ????? ??????? Visual Studio .NET ?????? ??? ??????? ??????? ????????.

    ?????? ??????? ???????? ??? ?????? ??? ????? ???? ????? SqlCommandBuilderDataAdapter ?????????? ?? ??????. ??? ??? ???? ??????? ??? ??????? (??????? ?? ????? ????????? ???????? ???? ????) ??? ?? ???? ???? ??????? ??????? ??????? ???????? ??????. ?????? ??? ??????? ??????? ??????? ? ??????? FillSchema ?? ????? ??????? MissingSchemaAction DataAdapter ????? ?? ??? AddWithKey ?? ?????? ?????? ??????? ??????? ?? ????????? ????????.

    ?? ???? ???????? ???????? ??????? ??? ???????? ???????? ?? ?????? 7.
    // 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 
    
  9. ???? ?? ???? ????, ?????? ??????? ??? ?????? DataRow.

    ?????? ????? ?????? ?????? ??????? ?????? ??? RemoveAt ???? ???? ??? ???? ???? ????? ?? ????? ???? ??? ?? ????????. ???? ??? ??? ??????? ????? ????? ?? ??? ????? ???????? ??????.

    ?? ???? ???????? ???????? ??????? ??? ???????? ???????? ?? ?????? ??? 8: DELETE //BEGIN
    //BEGIN DELETE CODE 
    
    drCurrent = tblAuthors->Rows->Find(new String("993-21-3427"));
    drCurrent->Delete();
    Console::WriteLine("Record deleted successfully, Click any key to continue!!"); 
    Console::ReadLine();
    
    //END DELETE CODE  
    
  10. ????? ????????? ??? ???? SQL ????? ??? ???? ??? ??????? ??????.

    ?? ???? ???????? ???????? ??????? ??? ???????? ???????? ?? ?????? ??? 9:
    // CLEAN UP SQL SERVER
    daAuthors->Update(dsPubs, "Authors");
    Console::WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
    Console::ReadLine();
    objConn->Close ();
    
  11. ??? ??????? ????? ??.
  12. ??????? ????? ? ???? ??? "????" ?????? ???????.

    ???? ???? ??? ?????? ??????? ???? ???? ??? ?????? ???????? ???????? ????? ?????? ?????? ??????? ???????? ????? ???? ????? ?? ????????? ????????.


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

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>
#using <System.Dll>
#using <System.Data.Dll>
#using <System.Xml.dll>
#include <tchar.h>

using namespace System;
using namespace System::Xml;
using namespace System::Data;
using namespace System::Data::SqlClient;

// This is the entry point for this application
int _tmain(void)
{
    SqlConnection  *objConn;
    try
    {
        String *sConnectionString;
        sConnectionString = "Password=StrongPassword;User ID=UserName;Initial Catalog=pubs;Data Source=(local)";

        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 = dsPubs->Tables->Item["Authors"];


        // Obtain a new DataRow object from the DataTable.
        DataRow* drCurrent = tblAuthors->NewRow();

        // Set the DataRow field values as necessary.
        drCurrent->set_Item("au_id",new String("993-21-3427"));

        drCurrent->set_Item("au_fname",new String("George"));
        drCurrent->set_Item("au_lname",new String("Johnson"));
        drCurrent->set_Item("phone",new String("800 226-0752"));
        drCurrent->set_Item("address", new String ("1956 Arlington Pl."));
        drCurrent->set_Item("city", new String("Winnipeg"));
        drCurrent->set_Item("state", new String("MB"));
        drCurrent->set_Item("contract",__box(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(new String("213-46-8915"));
        drCurrent->BeginEdit();

        drCurrent->set_Item("phone",String::Concat(S"342",(static_cast<String *>(drCurrent->Item["phone"]))->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(new String("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();
    }
    catch (Exception* ex)
    {
        Console::WriteLine (ex->Message );
    }
    __finally
    {
        objConn->Close ();
    }
}


?????

?????? ??? ???? ?? ????????? ??? ??????? ADO.NET ??????? DataSet SQL? ?? ?????? ????? Microsoft ??????? ??? ?????:

???????

???? ???????: 815660 - ????? ??? ??????: 29/???/1428 - ??????: 5.2
????? ???
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
????? ??????: 
kbmt kbsystemdata kbsqlclient kbhowtomaster kbhowto KB815660 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????815660

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

 

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