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年中加入常用的語言執行階段支援編譯器選項,請依照下列步驟執行:
// 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
//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
變更傳送到 SQL Server,以移除先前加入的記錄。
在步驟 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 ();
儲存您的專案。
在 [偵錯] 功能表上按一下 [開始] 執行專案]。
請注意,數個訊息方塊出現,指出程式碼的進度,並可讓您檢閱程式碼隨著資料的目前狀態。
完成程式碼清單
// 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 ();
}
}
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。