📄 dbtest.cpp
字号:
// DBTest.cpp : 能贾 览侩 橇肺弊伐俊 措茄 柳涝痢阑 沥狼钦聪促.
//
//#include "stdafx.h"
#include <conio.h> // for getch();
#include <time.h> // for time()
#include "DBDef.h"
#include "ADORs.h"
//-----------------------------------------------------------------------------
BYTE *g_bpArray = NULL;
BYTE *g_bpInven = NULL;
int g_iTestV = 0; // 烙矫蔼捞促.
//-----------------------------------------------------------------------------
_ConnectionPtr g_pConn1 = NULL;
TCHAR g_szConn[] = "PROVIDER=SQLOLEDB;SERVER=127.0.0.1;UID=test;PWD=test;DATABASE=TestDB";
//-----------------------------------------------------------------------------
bool InitParam(void); // DB 包访 檬扁拳
void CleanupDB(void); // DB 包访 辆丰 贸府
bool InitSocket(void);
void ConsoleProcess(void);
bool ParseMessage(char *cmd);
bool StartupServer(void);
bool CleanupServer(void);
void TestFuc(void);
void TestAddNew(void);
void TestUpdate(void);
void TestSelect(void);
//-----------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
if (!StartupServer())
{
getch();
return 1;
}
ConsoleProcess();
CleanupServer();
return 0;
}
//-----------------------------------------------------------------------------
bool StartupServer(void)
{
InitSocket();
if (!InitParam()) return false;
return true;
}
//-----------------------------------------------------------------------------
bool CleanupServer(void)
{
WSACleanup();
CleanupDB();
if( g_bpArray != NULL )
{
delete [] g_bpArray;
}
if( g_bpInven != NULL )
{
delete [] g_bpInven;
}
return true;
}
//-----------------------------------------------------------------------------
// Window Socket 阑 龋免窍扁 傈 Window Socket 檬扁拳.
bool InitSocket(void)
{
WORD wVer;
WSADATA wsaData;
wVer = MAKEWORD(2,2);
if (WSAStartup(wVer, &wsaData) != 0)
{
printf("Error while starting up socket library : %d\n", WSAGetLastError());
return false;
}
return true;
}
//-----------------------------------------------------------------------------
//
bool InitParam(void)
{
if( FAILED(::CoInitialize(NULL)) )
{
printf("Error while CoInitiallize !!!!\n");
return false;
}
if ( FAILED(g_pConn1.CreateInstance(__uuidof(Connection))) )
{
printf("Error while g_pConn1 CreatInstance !!!\n");
return false;
}
if ( FAILED(g_pConn1->Open(g_szConn, "", "", adConnectUnspecified)) )
{
printf("Error while g_pConn1 Open !!!\n");
return false;
}
printf("g_pConn1 DB Connect SUCCESS !!!\n");
int curState = GetState(g_pConn1->State);
// array 且寸
g_bpArray = new BYTE[100];
g_bpInven = new BYTE[6450];
return true;
}
//-----------------------------------------------------------------------------
//
void CleanupDB(void)
{
int curState;
curState = GetState(g_pConn1->State);
if( curState == 2 )
{
if ( FAILED(g_pConn1->Close()) )
{
printf("Error while g_pConn1 Clise() !!!\n");
}
printf("g_pConn1 DB Disconnect SUCCESS !!!\n");
}
::CoUninitialize();
}
//-----------------------------------------------------------------------------
int GetState(int intState)
{// 1: 摧塞 2: 凯覆
int ret;
switch(intState)
{
case adStateClosed:
// 摧腮 惑怕
ret = 1;
break;
case adStateOpen:
// 凯赴 惑怕
ret = 2;
break;
default:
ret = 0;
break;
}
return ret;
}
//-----------------------------------------------------------------------------
bool PrintProviderError(_ConnectionPtr pConn)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ErrorPtr pErr = NULL;
if( (pConn->Errors->Count) > 0)
{
long nCount = pConn->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(long i = 0; i < nCount; i++)
{
pErr = pConn->Errors->GetItem(i);
char szret[16];
wsprintf( szret,"%04x", pErr->Number );
if( !strcmp( szret, "80040e2f" ) )
{
printf("DB Error : Des[%s] \n", (LPCSTR)pErr->Description);
return false;
}
else
{
printf("DB Error : Num[%04x]\n Des[%s] \n", pErr->Number, (LPCSTR)pErr->Description);
}
}
}
return true;
}
//-----------------------------------------------------------------------------
void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
// Print Com errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}
//-----------------------------------------------------------------------------
int ReconnectDB(_ConnectionPtr pConn)
{// 0 : FAIL 1: SUCCESS
int curState;
curState = GetState(pConn->State);
if( curState == 2 )
{
if ( FAILED(pConn->Close()) )
{
printf("Error while pConn Clise() !!!\n");
return 0;
}
printf("pConn DB Disconnect SUCCESS !!!\n");
}
try
{
if ( FAILED(pConn->Open(g_szConn, "", "", adConnectUnspecified)) )
{
printf("Error while pConn Open !!!\n");
return 0;
}
printf("pConn DB Connect SUCCESS !!!\n");
}
catch (_com_error& e)
{
PrintProviderError(pConn);
PrintComError(e);
return 0;
}
return 1;
}
//-----------------------------------------------------------------------------
void ConsoleProcess(void)
{
char cmd[1024];
int carrot;
int c;
printf("Server console system activated.\n\n");
memset(cmd, 0, 1024);
carrot = 0;
printf("# ");
while (true)
{
c = getch();
if (c != 13)
{
cmd[carrot] = (char)c;
carrot++;
printf("%c", c);
if (carrot >= 1024)
{
printf("\nCommand buffer overflow\n");
memset(cmd, 0, 1024);
carrot = 0;
printf("\n# ");
}
}
else
{
if (ParseMessage(cmd) == false) break;
printf("\n# ");
memset(cmd, 0, 1024);
carrot = 0;
}
}
printf("\nPress any key....\n\n");
getch();
printf("\n\nZone Server console system deactivated.\n");
}
//-----------------------------------------------------------------------------
bool ParseMessage(char *cmd)
{
if (strcmp("quit", cmd) == 0)
{
return false;
}
else if (strcmp("all", cmd) == 0)
{
int curState;
curState = GetState(g_pConn1->State);
printf("\ng_pConn1 curState [%d] !!!\n", curState);
long tmp1,tmp2;
tmp1 = g_pConn1->GetCommandTimeout();
tmp2 = g_pConn1->GetConnectionTimeout();
printf("CommandTimeOut[%d] ConnectionTimeout[%d]\n", tmp1, tmp2); // 檬窜困
}
else if (strcmp("test1", cmd) == 0)
{
TestFuc();
}
else if (strcmp("testadd", cmd ) == 0 )
{
TestAddNew();
}
else if (strcmp("testup", cmd ) == 0 )
{
TestUpdate();
}
else if (strcmp("testse", cmd ) == 0 )
{
TestSelect();
}
return true;
}
//-----------------------------------------------------------------------------
void TestFuc(void)
{
// ADORs CharRs;
char szQuery[18];
try
{
// Delete
// sprintf(szQuery, "delete from testTB where _name ='hoon'");
// g_pConn1->Execute(szQuery, NULL, adExecuteNoRecords);
TEST_STRT st_info;
ZeroMemory( &st_info, sizeof(TEST_STRT) );
// Add New
{
ADORs CharRs;
sprintf(szQuery,"hoon%d", 1);
st_info.bFvalue = 1;
st_info.bSvalue = 2;
st_info.iTvalue = 3;
st_info.bArr[0] = 4;
SYSTEMTIME st;
GetLocalTime(&st);
DATE date = (COleDateTime)st;
CharRs.AddNewData(g_pConn1, g_bpArray, szQuery, 2 , 3, (UCHAR *)&st_info, date);
}
// Select
{
ADORs CharRs;
sprintf(szQuery,"hoon%d",1);
CharRs.OpenData(szQuery, g_pConn1);
}
// Update
{
ADORs CharRs;
sprintf(szQuery,"hoon%d",1);
st_info.bFvalue = 5;
st_info.bSvalue = 6;
st_info.iTvalue = 7;
st_info.bArr[0] = 8;
SYSTEMTIME st;
GetLocalTime(&st);
DATE date = (COleDateTime)st;
CharRs.UpdateData( g_pConn1, g_bpArray, szQuery, 5, 7, (UCHAR *)&st_info, date);
}
// Select
{
ADORs CharRs;
sprintf(szQuery,"hoon%d",1);
CharRs.OpenData(szQuery, g_pConn1);
}
}
catch (_com_error& e)
{
PrintProviderError(g_pConn1);
PrintComError(e);
if (!ReconnectDB(g_pConn1))
{
printf("DB ReConnect FAIL!!!!!!!!!!!\n");
}
else
{
printf("DB ReConnect Seccess!!!!!!!!!!!\n");
int curState = GetState(g_pConn1->State);
printf("######### Connection DB curState [%d] !!!\n", curState);
}
}
}
//------------------------------------------------------------------------------
void TestAddNew(void)
{
char szQuery[18];
try
{
printf("Add New Test 矫累\n");
DWORD dwpreTick, dwafterTick;
TEST_STRT st_info;
ZeroMemory( &st_info, sizeof(TEST_STRT) );
INDEX_STRT IndexStr;
ZeroMemory( &IndexStr, sizeof(INDEX_STRT));
int iValue = 0;
dwpreTick = GetTickCount();
while(true)
{
/* ADORs CharRs;
sprintf(szQuery,"hoon%d", iValue);
st_info.bFvalue = 1;
st_info.bSvalue = 2;
st_info.iTvalue = 3;
st_info.bArr[0] = 4;
SYSTEMTIME st;
GetLocalTime(&st);
DATE date = (COleDateTime)st;
CharRs.AddNewData(g_pConn1, g_bpArray,szQuery, 2 , 3, (UCHAR *)&st_info, date);
iValue++;
if( iValue == 10000 ) break;*/
// 1锅
/* ADORs CharRs;
sprintf(szQuery,"hoon%d", iValue);
CharRs.AddNewItem1Data(g_pConn1, szQuery);
iValue++;
if( iValue == 30000 ) break;*/
// 2锅
ADORs CharRs,Char2Rs;
sprintf(szQuery,"hoon%d", iValue);
// CharRs.AddNewChar1Data(g_pConn1, szQuery);
// Inven 沥焊 持扁
CharRs.OpenItem2Data(szQuery, g_pConn1, &IndexStr);
Char2Rs.UpdateChar1Data( g_pConn1, szQuery, &IndexStr);
iValue++;
if( iValue == 30000 ) break;
// 3锅
/* ADORs CharRs;
sprintf(szQuery,"hoon%d", iValue );
iValue++;
CharRs.AddNewChar2Data(g_pConn1, szQuery, g_bpInven);
if( iValue == 30000 ) break; // 3父*/
}
dwafterTick = GetTickCount();
printf("贸府 矫埃 [%lu]\n", dwafterTick - dwpreTick );
printf("Add New Test 辆丰\n");
}
catch (_com_error& e)
{
if( PrintProviderError(g_pConn1) )
{
PrintComError(e);
if (!ReconnectDB(g_pConn1))
{
printf("DB ReConnect FAIL!!!!!!!!!!!\n");
}
else
{
printf("DB ReConnect Seccess!!!!!!!!!!!\n");
printf("######### Connection DB curState [%d] !!!\n", GetState(g_pConn1->State));
}
}
}
}
//------------------------------------------------------------------------------
void TestUpdate(void)
{
char szQuery[18];
try
{
printf("Update Test 矫累[%d]\n",g_iTestV);
DWORD dwpreTick, dwafterTick;
TEST_STRT st_info;
ZeroMemory( &st_info, sizeof(TEST_STRT) );
// INVEN_STRT Inven;
int iValue = 0;
dwpreTick = GetTickCount();
g_iTestV = 11;
while(true)
{
/* sprintf(szQuery,"hoon%d",iValue);
ADORs CharRs;
CharRs.AddNewItem1Data( g_pConn1, szQuery );*/
// Update绰 给秦航
g_iTestV++;
ADORs CharRs;
sprintf(szQuery,"hoon%d",iValue);
st_info.bFvalue = g_iTestV;
st_info.bSvalue = g_iTestV;
st_info.iTvalue = g_iTestV;
st_info.bArr[0] = g_iTestV;
SYSTEMTIME st;
GetLocalTime(&st);
DATE date = (COleDateTime)st;
CharRs.UpdateData( g_pConn1, g_bpArray, szQuery, 5, g_iTestV, (UCHAR *)&st_info, date);
iValue++;
if( iValue == 10000 ) break;
}
dwafterTick = GetTickCount();
printf("贸府 矫埃 [%lu]\n", dwafterTick - dwpreTick );
printf("Update Test 辆丰\n");
}
catch (_com_error& e)
{
PrintProviderError(g_pConn1);
PrintComError(e);
int curState = GetState(g_pConn1->State);
if (!ReconnectDB(g_pConn1))
{
// 犁立加 胶纳临 惯积!!
printf("DB ReConnect FAIL!!!!!!!!!!!\n");
}
else
{
printf("DB ReConnect Seccess!!!!!!!!!!!\n");
printf("######### Connection DB curState [%d] !!!\n", GetState(g_pConn1->State));
}
}
}
//------------------------------------------------------------------------------
void TestSelect(void)
{
char szQuery[18];
try
{
printf("Select Test 矫累\n");
DWORD dwpreTick, dwafterTick;
TEST_STRT st_info;
ZeroMemory( &st_info, sizeof(TEST_STRT) );
INDEX_STRT IndexStr;
ZeroMemory( &IndexStr, sizeof(INDEX_STRT));
int iValue = 0;
dwpreTick = GetTickCount();
INVEN_STRT Inven;
while(true)
{
/* ADORs CharRs;
sprintf(szQuery,"hoon%d",iValue);
CharRs.OpenData(szQuery, g_pConn1);
iValue++;
if( iValue == 10000 ) break;*/
// 1锅
/* ADORs CharRs;
sprintf(szQuery,"hoon%d",iValue);
CharRs.OpenItem1Data( szQuery, g_pConn1, &Inven);
iValue++;
if( iValue == 10000 ) break;*/
// 2锅
/* ADORs CharRs;
for( int i = 0; i < 150; i++ )
{
iValue++;
IndexStr.dwIndex[i] = iValue;
}
CharRs.OpenIndexItem1Data(&IndexStr, g_pConn1, &Inven);
if( iValue == 10000 ) break;*/
// 3锅
ADORs CharRs;
sprintf(szQuery,"hoon%d",iValue);
CharRs.OpnemChar2Data( szQuery, g_pConn1, &Inven);
iValue++;
if( iValue == 10000 ) break;
}
dwafterTick = GetTickCount();
printf("贸府 矫埃 [%lu]\n", dwafterTick - dwpreTick );
printf("Select Test 辆丰\n");
}
catch (_com_error& e)
{
PrintProviderError(g_pConn1);
PrintComError(e);
if (!ReconnectDB(g_pConn1))
{
printf("DB ReConnect FAIL!!!!!!!!!!!\n");
}
else
{
printf("DB ReConnect Seccess!!!!!!!!!!!\n");
printf("######### Connection DB curState [%d] !!!\n", GetState(g_pConn1->State));
}
}
}
//------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -