You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
224 lines
4.5 KiB
224 lines
4.5 KiB
#include "ado.h"
|
|
#include <stdio.h>
|
|
#include <ctype.h>
|
|
#include <string.h>
|
|
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
#include <map>
|
|
#include <vector>
|
|
#include <sstream>
|
|
#include <fstream>
|
|
|
|
#ifdef WIN32
|
|
#include <io.h>
|
|
#include <direct.h>
|
|
#else
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
using namespace std;
|
|
|
|
_ConnectionPtr m_pConnection;
|
|
_RecordsetPtr m_pRecordset;
|
|
HRESULT hr;
|
|
|
|
bool open(char* username, char* password, char* dbname, char* ip)
|
|
{
|
|
try
|
|
{
|
|
::CoInitialize(NULL); //初始化COM环境
|
|
m_pConnection.CreateInstance(__uuidof(Connection)); //创建连接对象
|
|
char connStr[200] = "";
|
|
sprintf(connStr, "Provider=SQLOLEDB;Data Source=%s;Initial Catalog=%s;", ip, dbname);
|
|
cout << connStr << endl;
|
|
hr = m_pConnection->Open(connStr, username, password, -1);
|
|
if (hr != S_OK)
|
|
return true;
|
|
}
|
|
catch (_com_error e)
|
|
{
|
|
cout << e.Description() << endl;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
bool openMySql(char* username, char* password, char* dbname, char* ip)
|
|
{
|
|
try
|
|
{
|
|
::CoInitialize(NULL); //初始化COM环境
|
|
m_pConnection.CreateInstance(__uuidof(Connection)); //创建连接对象
|
|
char connStr[200] = "";
|
|
sprintf(connStr, ("DATABASE=%s;DSN=myodbc;OPTION=0;PWD=%s;PORT=0;SERVER=%s;UID=%s;"),
|
|
dbname, username,ip, password);
|
|
cout << connStr << endl;
|
|
hr = m_pConnection->Open(connStr, username, password, -1);
|
|
if (hr != S_OK)
|
|
return true;
|
|
}
|
|
catch (_com_error e)
|
|
{
|
|
cout << e.Description() << endl;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
void Split(string strArg, string spliter, vector<string>& ans)
|
|
{
|
|
ans.clear();
|
|
size_t index0 = 0;
|
|
string one_arg;
|
|
if (strArg.find_first_not_of(' ') == string::npos)
|
|
strArg = "";
|
|
while (strArg.size() > 0)
|
|
{
|
|
index0 = strArg.find_first_of(spliter);
|
|
if (index0 != string::npos)
|
|
{
|
|
one_arg = strArg.substr(0, index0);
|
|
strArg = strArg.substr(index0 + 1);
|
|
ans.push_back(one_arg);
|
|
}
|
|
else
|
|
{
|
|
ans.push_back(strArg);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
// 有输入参数的查询
|
|
int ado_QuerySQL(char *SQL, int inputValueCount, char ** inputValue, int * outputColumn, int * outputValueCount, char **** outputValue)
|
|
{
|
|
Fields * fields = NULL;
|
|
long ColCount = 0;
|
|
long RowCount = 0;
|
|
|
|
*outputColumn = 0;
|
|
*outputValueCount = 0;
|
|
|
|
m_pRecordset.CreateInstance(__uuidof(Recordset));
|
|
|
|
m_pRecordset->Open(SQL, m_pConnection.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdText);
|
|
|
|
hr = m_pRecordset->get_Fields(&fields); //得到记录集的字段集和
|
|
|
|
if (SUCCEEDED(hr))
|
|
{
|
|
RowCount = m_pRecordset->GetRecordCount();
|
|
|
|
ColCount = m_pRecordset->Fields->Count;
|
|
|
|
|
|
//fields->get_Count(&ColCount);
|
|
|
|
|
|
printf("查询出来的数量行%d\n", RowCount);
|
|
printf("查询出来的数量列%d\n", ColCount);
|
|
|
|
|
|
*outputValueCount = (int)RowCount;
|
|
*outputColumn = (int)ColCount;
|
|
}
|
|
|
|
if (*outputValueCount <= 0)
|
|
{
|
|
return 0;
|
|
}
|
|
int index = 0;
|
|
// 开始分配内存并且存储
|
|
*outputValue = (char ***)calloc(RowCount + 1, sizeof(char**));
|
|
while (!m_pRecordset->adoEOF)
|
|
{
|
|
(*outputValue)[index] = (char **)calloc(ColCount + 1, sizeof(char *));
|
|
for (long i = 0; i < ColCount; i++)
|
|
{
|
|
//printf("%d\n", i);
|
|
/*if (i > 0 && i < 4) {
|
|
continue;
|
|
}*/
|
|
BSTR bstrColName;
|
|
fields->Item[i]->get_Name(&bstrColName);
|
|
//printf("%d\n", i);
|
|
|
|
//printf("%d\n", i);
|
|
_variant_t taskStyle = NULL;
|
|
taskStyle = m_pRecordset->GetCollect(bstrColName);
|
|
|
|
// printf("%d\n", i);
|
|
if (taskStyle.vt != VT_NULL) {
|
|
const char* str = NULL;
|
|
|
|
|
|
|
|
_bstr_t bst_t = (_bstr_t)taskStyle;
|
|
|
|
str = (const char*)bst_t;
|
|
|
|
(*outputValue)[index][i] = (char *)calloc(strlen(str)+1, sizeof(char));
|
|
strcpy((*outputValue)[index][i], str);
|
|
|
|
}
|
|
else
|
|
{
|
|
//printf("NULL\n");
|
|
(*outputValue)[index][i] = (char *)calloc(10000, sizeof(char));
|
|
strcpy((*outputValue)[index][i], "");
|
|
}
|
|
|
|
|
|
}
|
|
m_pRecordset->MoveNext();///移到下一条记录
|
|
index++;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// 无输入参数的查询
|
|
int ado_QuerySQLNoInputParam(char *SQL, int * outputColumn, int * outputValueCount, char **** outputValue)
|
|
{
|
|
return ado_QuerySQL(SQL, 0, NULL, outputColumn, outputValueCount, outputValue);
|
|
}
|
|
|
|
// 无参数操作
|
|
int ado_ExecuteSQLNoInputParam(char *SQL)
|
|
{
|
|
return ado_ExecuteSQL(SQL, 0, NULL);
|
|
}
|
|
|
|
// 执行类操作
|
|
int ado_ExecuteSQL(char *SQL, int valueCount, char **value)
|
|
{
|
|
try
|
|
{
|
|
m_pConnection->Execute(_bstr_t(SQL), 0, adCmdText);
|
|
printf("删除结束\n");
|
|
//LogInsert ("写入结束!");
|
|
}
|
|
catch (_com_error e)
|
|
{
|
|
printf(e.Description());
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void close(void)
|
|
{
|
|
|
|
try
|
|
{
|
|
if (m_pRecordset != NULL) {
|
|
m_pRecordset->Close();
|
|
m_pConnection->Close();
|
|
}
|
|
printf("2222");
|
|
::CoUninitialize(); //释放环境
|
|
}
|
|
catch (_com_error e)
|
|
{
|
|
cout << e.Description() << endl;
|
|
}
|
|
} |