0 Replies - 638 Views - Last Post: 28 July 2009 - 05:28 PM Rate Topic: -----

#1 darmawan efendi  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 03-November 08

Problem with Insert into to access from C/C++

Posted 28 July 2009 - 05:28 PM

Hi I want to try insert data from C/C++ to acess..
In my code to connect database successful but to insert data failed..
description failed : syntax error in INSERT INTO statement.

this is my code


#import <C:\\Program Files\\Common Files\\System\\ado\\msado15.dll>  \
	rename( "EOF", "AdoNSEOF" )

#include <iostream>
#include <tchar.h>
#include <windows.h>
#include <stdio.h>

using namespace std;

// Data Access Method used in this sample
const char* DAM = "ADO";

// Connection string for ADO 
_bstr_t bstrConnect = 
	"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\lat.mdb;";

int _tmain(int argc, _TCHAR* argv[])
{
	// Initialize the Component Object Module Library (COM)
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr))
	{
		cout<<DAM<<": Failed to CoInitialize() COM."<<endl;
		return hr;
	}

	// COM errors are handled by C++ try/catch block
	try
	{
		ADODB::_ConnectionPtr pConn("ADODB.Connection");
		hr = pConn->Open(bstrConnect, "admin", "", ADODB::adConnectUnspecified);
		if (SUCCEEDED(hr))
		{
			cout<<DAM<<": Successfully connected to database. Data source name:\n  "
			   <<pConn->GetConnectionString()<<endl;

			// Prepare SQL query.
				   /*Initialize the values */
			  _bstr_t strName;
			  _bstr_t strpass;

			   strName = "'Darmawan',";
			   strpass = "'12345')";

			/* Append the values to the Insert Statement */
			_bstr_t strSQL("INSERT INTO Login(Username,Password) VALUES(");

			strSQL += strName + strpass;

			printf("%s\n",(LPCSTR)strSQL);

	   //Execute the insert statement
	  pConn->Execute((LPCSTR)strSQL,NULL,ADODB::adCmdText);
	   printf("Data Added Successfully\n",(LPCSTR)strSQL);

	}
		else
		{
			cout<<DAM<<": Unable to connect to data source: "<<bstrConnect<<endl;
		}
	}
	catch(_com_error& e)
	{
		cout<<DAM<<": _com_error: "<<e.Description()<<endl;
	}

	// Release the Component Object Module Library (COM)
	CoUninitialize();
	return hr;
}




I also attached my database
Thank You... :D

Attached File(s)

  • Attached File  lat.zip (9.97K)
    Number of downloads: 41


Is This A Good Question/Topic? 0
  • +

Page 1 of 1