´ÙÀ½ Áß Çϳª¸¦: Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server ¶Ç´Â Microsoft Windows NT 4.0 Server
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");
// 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 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
// CLEAN UP SQL SERVER
daAuthors->Update(dsPubs, "Authors");
Console::WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
Console::ReadLine();
objConn->Close ();
// 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 ();
}
}