Visual C++ .NET/Database ADO.net/DataSet
Содержание
Add tables to DataSet
#include "stdafx.h"
using namespace System;
using namespace System::Data;
void main()
{
DataSet^ myDataSet = gcnew DataSet("Game Data");
myDataSet->Tables->Add("Player");
myDataSet->Tables->Add("Score");
DataTable^ playerTable = myDataSet->Tables["Player"];
DataColumn^ firstNameColumn1 = playerTable->Columns->Add("First Name",String::typeid);
DataColumn^ lastNameColumn1 = playerTable->Columns->Add("Last Name",String::typeid);
playerTable->Columns->Add("Email", String::typeid);
}
Convert data table from database to xml
#include "stdafx.h"
using namespace System;
using namespace System::Configuration;
using namespace System::Data;
using namespace System::Data::SqlClient;
using namespace System::Xml;
void Navigate(XmlNode ^node, int depth)
{
if (node == nullptr)
return;
Console::WriteLine(depth);
Console::WriteLine(node->NodeType.ToString());
Console::WriteLine(node->Name);
Console::WriteLine(node->Value);
if (node->Attributes != nullptr)
{
for (int i = 0; i < node->Attributes->Count; i++)
{
Console::WriteLine(node->Attributes[i]->Name);
Console::WriteLine(node->Attributes[i]->Value);
}
}
Navigate(node->FirstChild, depth+1);
Navigate(node->NextSibling, depth);
}
void main(){
XmlDocument ^doc = gcnew XmlDocument();
SqlConnection ^connect = gcnew SqlConnection();
connect->ConnectionString = "SQLConnection";
SqlDataAdapter ^dAdapt = gcnew SqlDataAdapter();
DataSet ^dSet = gcnew DataSet();
dAdapt->SelectCommand = gcnew SqlCommand("SELECT * FROM Authors", connect);
dAdapt->Fill(dSet, "Authors");
XmlDataDocument ^doc1 = gcnew XmlDataDocument(dSet);
Navigate(doc1->DocumentElement, 0);
}
Create DataSet
#include "stdafx.h"
using namespace System;
using namespace System::Data;
void main()
{
DataSet^ myDataSet = gcnew DataSet("Game Data");
myDataSet->Tables->Add("Player");
myDataSet->Tables->Add("Score");
DataTable^ playerTable = myDataSet->Tables["Player"];
DataColumn^ firstNameColumn1 = playerTable->Columns->Add("First Name",String::typeid);
DataColumn^ lastNameColumn1 = playerTable->Columns->Add("Last Name",String::typeid);
playerTable->Columns->Add("Email", String::typeid);
}
Create XmlDataDocument from DataSet
#include "stdafx.h"
using namespace System;
using namespace System::Configuration;
using namespace System::Data;
using namespace System::Data::SqlClient;
using namespace System::Xml;
void Navigate(XmlNode ^node, int depth)
{
if (node == nullptr)
return;
Console::WriteLine(depth);
Console::WriteLine(node->NodeType.ToString());
Console::WriteLine(node->Name);
Console::WriteLine(node->Value);
if (node->Attributes != nullptr)
{
for (int i = 0; i < node->Attributes->Count; i++)
{
Console::WriteLine(node->Attributes[i]->Name);
Console::WriteLine(node->Attributes[i]->Value);
}
}
Navigate(node->FirstChild, depth+1);
Navigate(node->NextSibling, depth);
}
void main(){
XmlDocument ^doc = gcnew XmlDocument();
SqlConnection ^connect = gcnew SqlConnection();
connect->ConnectionString = "SQLConnection";
SqlDataAdapter ^dAdapt = gcnew SqlDataAdapter();
DataSet ^dSet = gcnew DataSet();
dAdapt->SelectCommand = gcnew SqlCommand("SELECT * FROM Authors", connect);
dAdapt->Fill(dSet, "Authors");
XmlDataDocument ^doc1 = gcnew XmlDataDocument(dSet);
Navigate(doc1->DocumentElement, 0);
}
Load DataSet
#include "stdafx.h"
using namespace System;
using namespace System::Data;
using namespace System::Data::OleDb;
void main()
{
OleDbConnection^ myConnection = nullptr;
myConnection = gcnew OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=UltraMax-Music.mdb");
myConnection->Open();
OleDbCommand^ myCommand = gcnew OleDbCommand("SELECT * FROM Music",myConnection);
OleDbDataReader^ myReader = myCommand->ExecuteReader();
DataSet^ myDataSet = gcnew DataSet();
array<String^>^ tables = {"Music"};
myDataSet->Load(myReader, LoadOption::OverwriteChanges, tables);
Console::WriteLine("{0} rows loaded",myDataSet->Tables[0]->Rows->Count);
myConnection->Close();
}