Visual C++ .NET/Database ADO.net/OleDbCommand

Материал из .Net Framework эксперт
Версия от 12:06, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Using database Commands

 
#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=Music.mdb");
    myConnection->Open();
    OleDbCommand^ myCommand = myConnection->CreateCommand();
    myCommand->CommandText = "SELECT COUNT(*) FROM Music";
    myCommand = gcnew OleDbCommand("SELECT COUNT(*) FROM Music",myConnection);
    Object^ count = myCommand->ExecuteScalar();
    Console::WriteLine("There are {0} songs in the Music table", count);
    myCommand->CommandText ="DELETE FROM Music WHERE Genre="N"";
    int RecordsAffected = myCommand->ExecuteNonQuery();
    Console::WriteLine("{0} songs were deleted from the Music table",RecordsAffected);
    myConnection->Close();
}


Using database Parameter

 
#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=Music.mdb");
    myConnection->Open();
    OleDbCommand^ MyCommand = gcnew OleDbCommand("SELECT COUNT(*) FROM Music WHERE Genre=@Genre",myConnection);
    OleDbParameter^ parameter = gcnew OleDbParameter("@Genre",OleDbType::Char);
    parameter->Direction = ParameterDirection::Input;
    parameter->Value = "Techno/Classical";
    MyCommand->Parameters->Add(parameter);
    Object^ Count = MyCommand->ExecuteScalar();
    Console::WriteLine("Count = {0}", Count);
    myConnection->Close();
}