Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C++/CLI

OLE DB - First steps

Rate me:
Please Sign up or sign in to vote.
4.97/5 (31 votes)
1 Dec 2001CPOL2 min read 338.1K   3.8K   29  
Basic introduction to using OLE DB to insert, update and read records from a database
#include "stdafx.h"

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

using namespace System;
using namespace System::Data::OleDb;

void InsertRecord(String* nam, int age);
void ShowAge(String* nam);

int _tmain(void)
{
	
	InsertRecord("Tom",25);
	InsertRecord("Harry",26);
	InsertRecord("Jasonb",24);
	InsertRecord("Andy",26);

	ShowAge("Tom");
	ShowAge("Andy");
	ShowAge("Henry");

    return 0;
}

void InsertRecord(String* nam, int age)
{
	OleDbConnection* conn = new OleDbConnection(
		"PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\test.mdb");

	conn->Open();

	String* sqlstr = String::Format("insert into main(Name,Age) values('{0}',{1})",
		nam,__box(age));

	OleDbCommand* cmd = new OleDbCommand(sqlstr,conn);

	cmd->ExecuteNonQuery();

	conn->Close();
}

void ShowAge(String* nam)
{
	OleDbConnection* conn = new OleDbConnection(
		"PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\test.mdb");

	conn->Open();

	String* sqlstr = String::Format("select age from main where Name = '{0}'",nam);

	OleDbCommand* cmd = new OleDbCommand(sqlstr,conn);

	OleDbDataReader* rdr = cmd->ExecuteReader();

	if(rdr->Read())
	{
		Console::WriteLine("{0} is {1} years old",
			nam,rdr->get_Item("Age"));
	}
	else
	{
		Console::WriteLine("No matches found for {0}!",nam);
	}

	conn->Close();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions