Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The code throws an exception on attempting to execute the indicated line ( at least so it appears from my debugging effort.Actually, I have never written and run a database creation code before. I realise I needed it and I wrote this and decided to test it before modifying to become the full fledged code.

I am using Sql server ce 3.5 that came along with visual studio 2010

The try clause I included in the code generated the following output error messages:
Description: Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available.No work was done.

Message: IDispatch error $3105
(see catch clause below)
C++
// Maga.cpp : Defines the entry point for the console application.
//

#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")
void CreateNewDataBase(TCHAR *szConnString,TCHAR *szClassName,TCHAR *szNewPassword)
{
    //::CoInitializeEx(NULL,COINIT_MULTITHREADED);
    
    _ConnectionPtr Conn = NULL;
    _CommandPtr Cmd = NULL;
    
    try
    {
    	//Create an open connection
    	Conn.CreateInstance(__uuidof(Connection));
    
    	Conn->ConnectionString = _bstr_t(szConnString);
    	
//Exception thrown while trying to execute the open statement
    	Conn->Open(_bstr_t(L""),_bstr_t(L""),_bstr_t(L""),-1);
    
    	TCHAR szString[1000];
    	StringCbPrintf(szString,sizeof(szString),_T("CREATE DATABASE \"%s\" DATABASE PASSWORD '%s' COLLATE Latini_General_CI_AS"),szClassName,szNewPassword);
    	
    	//Create a new database
    	Cmd.CreateInstance(__uuidof(Command));
    	Cmd->ActiveConnection = Conn;
    	Cmd->CommandText = _bstr_t(szString); 
    	Cmd->Execute(NULL,NULL,adCmdText);
    
    	//Create SchInfo table
    	StringCbPrintf(szString,sizeof(szString),_T("%s %s"),
    	  _T("CREATE TABLE SchInfo (SchInfoID int NOT NULL, 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), Phone nvarchar(20));"));
    	Cmd->CommandText = _bstr_t(szString);
    	Cmd->Execute(NULL,NULL,adCmdText);
    	//Create Primary Key
    	StringCbPrintf(szString,sizeof(szString),_T("%s"),
    		_T("ALTER TABLE SchInfo ADD (CONSTRAINT PK_SchInfo PRIMARY KEY (SchInfoID) AUTO INCREMENT);"));
    	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();
    	TCHAR szString[1000];
    	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();
    	}
    	throw 2.0;
    }
}

//::CoUninitialize();
int _tmain(int argc, _TCHAR* argv[])
{
    ::CoInitializeEx(NULL,COINIT_MULTITHREADED);
    TCHAR szDataSource[] =_T("MyDatabase#1.sdf");
    TCHAR szPassword[] = _T("XXXXXXXXXXXXXXXX");
    TCHAR szClassName[] =_T("NewClass");
    TCHAR szNewPassword[] = _T("NewPassword");
    TCHAR szConnString[500];
    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'; "),szDataSource,szPassword);
    CreateNewDataBase(szConnString,szClassName,szNewPassword);
    ::CoUninitialize();
    MessageBox(NULL,_T("Succeded"),_T("Succeeded"),MB_OK);
    return 0;
}
Posted
Updated 9-Sep-14 18:19pm
v5
Comments
George Jonsson 10-Sep-14 0:11am    
Do you know which line it fails on?
What happens if you only execute the first Cmd->Execute(NULL,NULL,adCmdText); and comment out the rest of them?
Gbenbam 10-Sep-14 0:29am    
Yes I know the failure line.I have included a comment to show it.It fails while attempting to execute the open command. I 'II try your suggestion
George Jonsson 10-Sep-14 0:51am    
Hmm, are you sure your created connection string is correct?
This article might help you along ADO at a glance[^]
Richard MacCutchan 10-Sep-14 3:22am    
Too many unknowns in your code. Show the actual value of your connection string, and the details of the exception.
Gbenbam 10-Sep-14 5:21am    
pls try to have another look.There are no unknown.Every thing has neccesssy argument has been included in the main() function.

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