请问这个我的数据库程序有什么问题 [英] Please what is wrong with this my database program

查看:61
本文介绍了请问这个我的数据库程序有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用所声明的连接字符串,以下代码可以工作(除了CREATE TABLE查询),但如果我使用注释掉的连接字符串则无法工作。



通常,无论我是否使用已注释掉的查询,CreateNewDataBase函数都有效。



但一般问题是所有需要密码的连接字符串,最大缓冲区大小或最大数据库大小无法工作,而是抛出异常。

抛出异常的详细信息如下:

描述:生成多步OLE BD操作errors.Check每个OLE DB值(如果可用)。没有工作;

消息:IDispatch错误#3105



可能是因为我使用的是sql server ce 3的版本, 5附带VS?



除此之外,当我使用不包含密码,最大缓冲区大小和最大数据库大小的连接字符串时,程序运行但是尝试执行CREATE TABLE查询时出现异常。

引发的异常的详细信息是:

描述:(null);

消息:指针无效。



请问我的CREATE TABLE查询有什么问题?



PS:

我已经搜索了IDispatch错误#3105,但没有得到满意的信息。















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.







#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;
}

推荐答案

这与您之前的问题基本相同:请问这个我的c ++新数据库创建代码出了什么问题? [ ^ ]。



然而在声明中:

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:
Cmd->Execute(NULL,NULL,adCmdText);



在哪里宣布 adCmdText 它应该是什么?



我注意到你也没有检查所有COM调用的结果代码,如上一个问题所示。为初始测试尝试更简单的数据库结构可能是个好主意,然后添加额外的参数和选项直到出现故障,看看哪个项目导致问题。


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.


这篇关于请问这个我的数据库程序有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆