用C++ 通过ADO方式访问SQLServer 2000

soft_jianbiao 2008-10-29 09:01:19
vector<string> RunProcRS(char *procName, _ParameterPtr *Parameters,int size)
{
_CommandPtr pCommand;
_RecordsetPtr pRecordset;
_variant_t var;
string str_value;
string record;
vector<string> Result;
int count = 0;
Open(dbName,User,Password);
try
{
pCommand.CreateInstance(__uuidof(Command));
pCommand->ActiveConnection = m_pConnection;
pCommand->CommandText = procName;
pCommand->CommandType = adCmdStoredProc;
for(int i = 0; i < size; i++)
{
pCommand->Parameters->Append(Parameters[i]);
}
pRecordset = pCommand->Execute(NULL,NULL,adCmdStoredProc);

//取得字段列表
FieldsPtr pFields = pRecordset->Fields;

count = pFields->GetCount();

while(!pRecordset->EndOfFile)
{

record = "";

for(int i = 0; i < count; i++)
{

var = pRecordset->GetCollect(_variant_t((long)i));
if(var.vt != VT_NULL)
{
str_value = (LPSTR)_bstr_t(var);
record = record + str_value + " ";
}
}
Result.push_back(record);
pRecordset->MoveNext();
}
pRecordset->Close();

}
catch(_com_error e)
{
string ErrorMessage(e.ErrorMessage());
string ErrorType("储存过程操作出错!\r\n错误信息: ");
string ErrorTime(GetTime());
string CRLF("\r\n\r\n");
PrintProviderError(ErrorTime + ErrorType + ErrorMessage + CRLF);
return false;
}
Close();
return Result;
}

这是一个通过存储过程返回一个记录集的方法,
但一旦我的存储过程中 有几条SQL语句的时候总是出问题(比如一条insert,一条select)!
不知道为社么?望高手指点,小弟雪地里跪求!,如果要整个类就我就贴出来!
...全文
92 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
soft_jianbiao 2008-10-29
  • 打赏
  • 举报
回复
///////////////////////////////////////////////////////////
// //
// CreateParameter Function //
// //
///////////////////////////////////////////////////////////
_ParameterPtr CreateParameter(_bstr_t btParameterName,
DataTypeEnum enDataType,
ParameterDirectionEnum enParameterDirection,
long lSize,
char*Value )
{
_variant_t vtValue(Value);
_CommandPtr pCommand;
_ParameterPtr pParameter;
Open(dbName,User,Password);
try
{
pCommand.CreateInstance(__uuidof(Command));
pParameter.CreateInstance(__uuidof(Parameter));
pCommand->ActiveConnection = m_pConnection;
pParameter = pCommand->CreateParameter( btParameterName,
enDataType,
enParameterDirection,
lSize,
vtValue );
}
catch(_com_error e)
{
string ErrorMessage(e.ErrorMessage());
string ErrorType("生成储存过程参数操作出错!\r\n错误信息: ");
string ErrorTime(GetTime());
string CRLF("\r\n\r\n");
PrintProviderError(ErrorTime + ErrorType + ErrorMessage + CRLF);
return NULL;
}
Close();
return pParameter;
}

///////////////////////////////////////////////////////////
// //
// RunProc Function //
// //
///////////////////////////////////////////////////////////
bool RunProc(char *procName, _ParameterPtr *Parameters,int size, char *value)
{
_CommandPtr pCommand;
Open(dbName,User,Password);
try
{
pCommand.CreateInstance(__uuidof(Command));
pCommand->ActiveConnection = m_pConnection;
pCommand->CommandText = procName;
pCommand->CommandType = adCmdStoredProc;
for(int i = 0; i < size; i++)
{
pCommand->Parameters->Append(Parameters[i]);
}
pCommand->Execute(NULL,NULL,adCmdStoredProc);
}
catch(_com_error e)
{
string ErrorMessage(e.ErrorMessage());
string ErrorType("储存过程操作出错!\r\n错误信息: ");
string ErrorTime(GetTime());
string CRLF("\r\n\r\n");
PrintProviderError(ErrorTime + ErrorType + ErrorMessage + CRLF);
return false;
}
strcpy(value ,(char*)_bstr_t(Parameters[size - 1]->Value));
Close();
return true;
}

///////////////////////////////////////////////////////////
// //
// RunProc Function //
// //
///////////////////////////////////////////////////////////
bool RunProc(char *procName, _ParameterPtr *Parameters,int size)
{
_CommandPtr pCommand;
Open(dbName,User,Password);
try
{
pCommand.CreateInstance(__uuidof(Command));
pCommand->ActiveConnection = m_pConnection;
pCommand->CommandText = procName;
pCommand->CommandType = adCmdStoredProc;
for(int i = 0; i < size; i++)
{
pCommand->Parameters->Append(Parameters[i]);
}
pCommand->Execute(NULL,NULL,adCmdStoredProc);
}
catch(_com_error e)
{
string ErrorMessage(e.ErrorMessage());
string ErrorType("储存过程操作出错!\r\n错误信息: ");
string ErrorTime(GetTime());
string CRLF("\r\n\r\n");
PrintProviderError(ErrorTime + ErrorType + ErrorMessage + CRLF);
return false;
}
return true;
}

///////////////////////////////////////////////////////////
// //
// RunProRSc Function //
// //
///////////////////////////////////////////////////////////
vector<string> RunProcRS(char *procName, _ParameterPtr *Parameters,int size)
{
_CommandPtr pCommand;
_RecordsetPtr pRecordset;
_variant_t var;
string str_value;
string record;
vector<string> Result;
int count = 0;
Open(dbName,User,Password);
try
{
pCommand.CreateInstance(__uuidof(Command));
pCommand->ActiveConnection = m_pConnection;
pCommand->CommandText = procName;
pCommand->CommandType = adCmdStoredProc;
for(int i = 0; i < size; i++)
{
pCommand->Parameters->Append(Parameters[i]);
}
pRecordset = pCommand->Execute(NULL,NULL,adCmdStoredProc);

//取得字段列表
FieldsPtr pFields = pRecordset->Fields;

count = pFields->GetCount();

while(!pRecordset->EndOfFile)
{

record = "";

for(int i = 0; i < count; i++)
{

var = pRecordset->GetCollect(_variant_t((long)i));
if(var.vt != VT_NULL)
{
str_value = (LPSTR)_bstr_t(var);
record = record + str_value + " ";
}
}
Result.push_back(record);
pRecordset->MoveNext();
}
pRecordset->Close();

}
catch(_com_error e)
{
string ErrorMessage(e.ErrorMessage());
string ErrorType("储存过程操作出错!\r\n错误信息: ");
string ErrorTime(GetTime());
string CRLF("\r\n\r\n");
PrintProviderError(ErrorTime + ErrorType + ErrorMessage + CRLF);
return false;
}
Close();
return Result;
}
};

#ifdef __cplusplus
}
#endif

#endif
soft_jianbiao 2008-10-29
  • 打赏
  • 举报
回复
public:

///////////////////////////////////////////////////////////
// //
// Construction //
// //
///////////////////////////////////////////////////////////
DataBase(char *db, char *user, char *password)
{

dbName = db;
User = user;
Password = password;
} //end of DataBase

///////////////////////////////////////////////////////////
// //
// Insert Function //
// //
///////////////////////////////////////////////////////////
bool Insert(char *Sql)
{
Open(dbName,User,Password);
try
{
m_pConnection->Execute((_bstr_t)Sql, NULL, adCmdText);
}
catch(_com_error e)
{
string ErrorMessage(e.ErrorMessage());
string ErrorType("数据插入操作出错!\r\n错误信息: ");
string ErrorTime(GetTime());
string CRLF("\r\n\r\n");
PrintProviderError(ErrorTime + ErrorType + ErrorMessage + CRLF);
return false;
}
Close();
return true;
}// end of Insert

///////////////////////////////////////////////////////////
// //
// Delete Function //
// //
///////////////////////////////////////////////////////////
bool Delete(char *Sql)
{
Open(dbName,User,Password);
try
{
m_pConnection->Execute((_bstr_t)Sql, NULL, adCmdText);
}
catch(_com_error e)
{
string ErrorMessage(e.ErrorMessage());
string ErrorType("数据删除操作出错!\r\n错误信息: ");
string ErrorTime(GetTime());
string CRLF("\r\n\r\n");
PrintProviderError(ErrorTime + ErrorType + ErrorMessage + CRLF);
return false;
}
Close();
return true;
}// end of Delete

///////////////////////////////////////////////////////////
// //
// Update Function //
// //
///////////////////////////////////////////////////////////
bool Update(char *Sql)
{
Open(dbName,User,Password);
try
{
m_pConnection->Execute((_bstr_t)Sql, NULL, adCmdText);
}
catch(_com_error e)
{
string ErrorMessage(e.ErrorMessage());
string ErrorType("数据更新操作出错!\r\n错误信息: ");
string ErrorTime(GetTime());
string CRLF("\r\n\r\n");
PrintProviderError(ErrorTime + ErrorType + ErrorMessage + CRLF);
return false;
}
Close();
return true;
}// end of Update

///////////////////////////////////////////////////////////
// //
// Select Function //
// //
///////////////////////////////////////////////////////////
vector<string> Select(char *Sql)
{
string strSql(Sql);
vector<string> Result;
string record;
_RecordsetPtr pRecordset;
_variant_t var;
string str_value;
int count = 0;
int i = 0;
Open(dbName,User,Password);
try
{
pRecordset = m_pConnection->Execute((_bstr_t)strSql.c_str(), NULL, adCmdText);

FieldsPtr pFields = pRecordset->Fields;
count = pFields->GetCount();

while( !pRecordset->EndOfFile )
{
record = "";
for(i = 0; i < count; i++)
{
var = pRecordset->GetCollect(_variant_t((long)i));
if(var.vt != VT_NULL)
{
str_value = (LPSTR)_bstr_t(var);
record = record + str_value + " ";
}
}
Result.push_back(record);
pRecordset->MoveNext();
}
pRecordset->Close();
}
catch(_com_error e)
{
string ErrorMessage(e.ErrorMessage());
string ErrorType("选择查询操作出错!\r\n错误信息: ");
string ErrorTime(GetTime());
string CRLF("\r\n\r\n");
PrintProviderError(ErrorTime + ErrorType + ErrorMessage + CRLF);
return false;
}
Close();
return Result;
}



///////////////////////////////////////////////////////////
// //
// GetRecordCount Function //
// //
///////////////////////////////////////////////////////////
int GetRecordCount(char* SqlAndWhere)
{
string Sql;
_RecordsetPtr pRecordset;
int Count = 0;
Open(dbName,User,Password);
Sql = "select *from "+ string(SqlAndWhere);
try
{
pRecordset = m_pConnection->Execute((_bstr_t)Sql.c_str(), NULL, adCmdText);
while(!pRecordset->EndOfFile)
{
Count++;
pRecordset->MoveNext();
}
pRecordset->Close();
}
catch(_com_error e)
{
string ErrorMessage(e.ErrorMessage());
string ErrorType("获得记录数操作出错!\r\n错误信息: ");
string ErrorTime(GetTime());
string CRLF("\r\n\r\n");
PrintProviderError(ErrorTime + ErrorType + ErrorMessage + CRLF);
return -1;
}
Close();
return Count;
} //end of GetRecordCount

soft_jianbiao 2008-10-29
  • 打赏
  • 举报
回复
这个是整个类!
#ifndef _DATABASE_H
#define _DATABASE_H
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF","EndOfFile")
#include <string>
#include <vector>
#include <time.h>
#ifdef _DEBUG
#include <iostream.h>
#endif
using namespace std;


///////////////////////////////////////////////////////////
// //
// GetTime Function //
// //
///////////////////////////////////////////////////////////
char* GetTime()
{
time_t now;
now = time(0);
struct tm *tnow = localtime(&now);
char *time = (char*)malloc(15 * sizeof(char));
sprintf(time,
"%d年%d月%d日%d时%d分%d秒r\n",
1900+tnow->tm_year,
tnow->tm_mon+1,
tnow->tm_mday,
tnow->tm_hour,
tnow->tm_min,
tnow->tm_sec);
return time;
}


//////////////////////////////////////////////////////////
// //
// GetKeyValue Function //
// //
///////////////////////////////////////////////////////////
void GetKeyValue(char *section, char *Key, char *value)
{

char buffer[100] = {'\0'};

GetModuleFileName(NULL,buffer,100);
string path(buffer);
path = path.substr(0,path.find_last_of("\\"));
path += "\\Config.ini";
GetPrivateProfileString(
section,
Key,
"",
value,
20,
path.c_str());
}


BOOL WriteMessage(HANDLE hFile, char *Message,BOOL flag)
{
DWORD MsgSize = 0;
DWORD nRead = 0;
int i = 0;
char *p = Message;
for(i = 0; *Message != '\0'; Message++,i++);
MsgSize = i;
Message = p;

if(flag)
{
DWORD fSize = 0;
fSize = GetFileSize(hFile,NULL);
SetFilePointer(hFile,fSize - 1,NULL,FILE_BEGIN);
}
if( !WriteFile(hFile,
Message,
MsgSize,
&nRead,
NULL) )
return FALSE;
return TRUE;
}

#ifdef __cplusplus
extern "C" {
#endif

class DataBase
{
private:
_ConnectionPtr m_pConnection ;
_RecordsetPtr m_pRecordset ;
char* dbName ;
char* User ;
char* Password ;


///////////////////////////////////////////////////////////
// //
// Open Function //
// //
///////////////////////////////////////////////////////////
bool Open(char *db, char *user, char *password)
{
CoInitialize(NULL); //初始化COM
HRESULT hResult = NULL;
bool Successed = false;
try
{
hResult = m_pConnection.CreateInstance(__uuidof(Connection));
if( SUCCEEDED( hResult ) )
{
string ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=" + string(db) + ";Data Source=127.0.0.1,1433";
m_pConnection->ConnectionString = ConnectionString.c_str();
m_pConnection->Open("", user, password, adConnectUnspecified);
} //end of if
Successed = true;
} //end of try
catch(_com_error e)
{
string ErrorMessage(e.ErrorMessage());
string ErrorType("数据库连接操作出错!\r\n错误信息: ");
string ErrorTime(GetTime());
string CRLF("\r\n\r\n");
PrintProviderError(ErrorTime + ErrorType + ErrorMessage + CRLF);
return false;
}
if(Successed)
return true;
else
return false;
} //end of Open

///////////////////////////////////////////////////////////
// //
// Close Function //
// //
///////////////////////////////////////////////////////////
bool Close()
{
if(m_pConnection->State)
{
try
{
m_pConnection->Close();
}
catch(_com_error e)
{
string ErrorMessage(e.ErrorMessage());
string ErrorType("数据库连接关闭出错!\r\n错误信息: ");
string ErrorTime(GetTime());
string CRLF("\r\n\r\n");
PrintProviderError(ErrorTime + ErrorType + ErrorMessage + CRLF);
return false;
}
}
m_pConnection = NULL;
return true;
}// end of close

///////////////////////////////////////////////////////////
// //
// PrintProviderError Function //
// //
///////////////////////////////////////////////////////////

void PrintProviderError(string ErrorMessage)
{
HANDLE hFile = CreateFile("Error.log",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
WriteMessage(hFile,(char*)ErrorMessage.c_str(),TRUE);
CloseHandle(hFile);
}//end of PrintProviderError


///////////////////////////////////////////////////////////
// //
// parseSql Function //
// //
///////////////////////////////////////////////////////////
/*int parseSql(char *Sql)
{
int count = 0;
while(hasNextString(Sql))
{
if( !strcmp(getString(Sql,",").c_str(),"from") )
break;
count ++;
}
return count;
}*/

22,296

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧