Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The following code works ( apart from the CREATE TABLE query) if I use the stated connection string, but fails to work if I use the commented out connection strings.

Generally the CreateNewDataBase function works whether I use the commented out query or not.

But the general problem is that all connection strings that require a Password,a Max Buffer Size or Max Database Size fails to work, instead it throws an exception.
The detail of the exception thrown is as follows:
Description:Multiple-Step OLE BD operation generated errors.Check each OLE DB value, if available. No work was done;
Message:IDispatch error #3105

Could it be because I am using the version of sql server ce 3,5 that comes with VS?

Apart from this, when I use the connection string that does not include Password , Max Buffer Size and Max Database size, the program runs but trows an exception while trying to execute the CREATE TABLE query.
The detail of the exception thrown is:
Description:(null);
Message: Invalid pointer.

Please what is wrong with my CREATE TABLE query?

P.S.:
I have googled over the IDispatch error #3105 but have gotten no satisfactory information.







C++
#include "stdafx.h"
#include<Ole2.h>
#include<tchar.h>
#include<strsafe.h>
#import "C:\Program Files\Common Files\System\Ado\msado15.dll" no_namespace rename("EOF","EndOfFile")

int CreateNewDataBase(TCHAR *szConnString,TCHAR *szNewDataSource,TCHAR *szNewPassword)
{
	TCHAR szString[1000];
	_ConnectionPtr Conn = NULL;
	_CommandPtr Cmd = NULL;
	
	try
	{
		//Create an open connection
		Conn.CreateInstance(__uuidof(Connection));

		Conn->ConnectionString = _bstr_t(szConnString);
		Conn->Open(_bstr_t(L""),_bstr_t(L""),_bstr_t(L""),-1);

		//StringCbPrintf(szString,sizeof(szString),_T("CREATE DATABASE \"%s\" DATABASEPASSWORD '%s' COLLATE Latin1_General_CI_AS"),szNewDataSource,szNewPassword);
		StringCbPrintf(szString,sizeof(szString),_T("CREATE DATABASE \"%s\""),szNewDataSource);
		
		//Create a new database
		Cmd.CreateInstance(__uuidof(Command));
		Cmd->ActiveConnection = Conn;
		Cmd->CommandText = _bstr_t(szString); 
		Cmd->Execute(NULL,NULL,adCmdText);

		Conn->Close();
	}
	catch(_com_error &e)
	{

		_bstr_t Error = e.Description();
		TCHAR *szDescription = (TCHAR *)Error;
		const TCHAR * szError = e.ErrorMessage();
		StringCbPrintf(szString,sizeof(szString),_T("Description : %s ; Message : %s"),szDescription,szError);
		//IErrorInfo *IError = e.ErrorInfo();
		MessageBox(NULL,szString,_T("Class Creation Error"),MB_OK);
		if(Conn)
		{
			Conn->Close();
		}
		return 0;
	}
	return 1;
}

int CreateTables(TCHAR *szConnString,int iTermID,int iTestID)
{
	_ConnectionPtr Conn = NULL;
	_CommandPtr Cmd = NULL;

	TCHAR szString[1000];
	
	try
	{
		//Create an open connection
		Conn.CreateInstance(__uuidof(Connection));

		Conn->ConnectionString = _bstr_t(szConnString);
		Conn->Open(_bstr_t(L""),_bstr_t(L""),_bstr_t(L""),-1);

		//Create SchInfo tableNOT NULL UNIQUE PRIMARY KEY
		StringCbPrintf(szString,sizeof(szString),_T("%s %s"),
		 _T("CREATE TABLE SchInfo (SchInfoID int IDENTITY(1,1) NOT NULL UNIQUE PRIMARY KEY, Name nvarchar(200) NOT NULL,Logo nvarchar(500),Street nvarchar (50) NOT NULL, Locality nvarchar(50) NOT NULL,"),
		 _T("City nvarchar(50) NOT NULL, State nvarchar(50) NOT NULL, Country nvarchar(50) NOT NULL, EMail nvarchar(50) UNIQUE, Phone nvarchar(20) UNIQUE);"));
		
		Cmd->CommandText = _bstr_t(szString);
		Cmd->Execute(NULL,NULL,adCmdText);
		
		Conn->Close();
	}
	catch(_com_error &e)
	{

		_bstr_t Error = e.Description();
		TCHAR *szDescription = (TCHAR *)Error;
		const TCHAR * szError = e.ErrorMessage();
		StringCbPrintf(szString,sizeof(szString),_T("Description : %s ; Message : %s"),szDescription,szError);
		//IErrorInfo *IError = e.ErrorInfo();
		MessageBox(NULL,szString,_T("Class Creation Error"),MB_OK);
		if(Conn)
		{
			Conn->Close();
		}
		return 0;
	}
	return 1;
}

int main()
{
	::CoInitializeEx(NULL,COINIT_MULTITHREADED);

	TCHAR szNewDataSource[] = _T("Test.sdf");
	TCHAR szNewPassword[] = _T("Password");

	TCHAR szOldDataSource[] = _T("C:\\Users\\user\\Documents\\MyDataBase.sdf");

	int iTermID = 1, iTestID = 1;
	// Password = '%s'; File Mode = 'shared read'; Max Database Size = '256'; Max Buffer Size = '256'
	TCHAR szOldPassword[] = _T("OldPassword");
	TCHAR szConnString[1000];
	//StringCbPrintf(szConnString,sizeof(szConnString),_T("Provider='Microsoft.SQLSERVER.CE.OLEDB.3.5';Persist Security Info='FALSE';Data Source='%s';Password = '%s'; File Mode = 'shared read'; Max Database Size = '256'; Max Buffer Size = '256"),szOldDataSource,szOldPassword);
	StringCbPrintf(szConnString,sizeof(szConnString),_T("Provider='Microsoft.SQLSERVER.CE.OLEDB.3.5';Persist Security Info='FALSE';Data Source='%s';"),szOldDataSource);

	if(CreateNewDataBase(szConnString,szNewDataSource,szNewPassword))
	{
		//StringCbPrintf(szConnString,sizeof(szConnString),_T("Provider = 'Microsoft.SQLSERVER.CE.OLEDB.3.5'; Persist Security Info = 'FALSE'; Data Source = '%s'; Password = '%s'; File Mode = 'shared read'; Max Database Size = '4000'; Max Buffer Size = '1024'; "),szNewDataSource,szNewPassword);
		StringCbPrintf(szConnString,sizeof(szConnString),_T("Provider = 'Microsoft.SQLSERVER.CE.OLEDB.3.5'; Persist Security Info = 'FALSE'; Data Source = '%s';"),szNewDataSource);
		if(CreateTables(szConnString,iTermID,iTestID))
		{
			MessageBox(NULL,_T("New database and table were successfully created."),_T("Database Creation Success"),MB_OK);
		}
	}
	else
	{
		MessageBox(NULL,_T("Could not create database and table."),_T("Database Creation Failure"),MB_OK);
	}
	
	::CoUninitialize();
	
	return 1;
}
Posted
Updated 11-Sep-14 3:44am
v2

1 solution

This is essentially the same as your previous question: Please what is wrong with this my c++ new database creation code?[^].

However in the statement:
C++
Cmd->Execute(NULL,NULL,adCmdText);

where is adCmdText declared and what is it supposed to be?

I notice that you are also not checking the result codes from all the COM calls, as suggested in the previous question. It would probably be a good idea to try a much simpler database structure for your initial testing, and then add extra parameters and options until you get failures, to see which item is causing the problem.
 
Share this answer
 
v2
Comments
Gbenbam 11-Sep-14 10:36am    
I thought adCmdText is defined in the added header files or is it not?
Richard MacCutchan 11-Sep-14 10:37am    
How woud I know?
Gbenbam 11-Sep-14 10:49am    
adCmdText equal 1.Intellisence recognise it.
Richard MacCutchan 11-Sep-14 10:50am    
Fine, but what does that mean in the context of your command?
Gbenbam 11-Sep-14 10:44am    
I have tried a much simpler connection that uses a password but the code code only runs when password is excluded.

But I need a passworded database.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900