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.
221 lines
4.2 KiB
221 lines
4.2 KiB
//#include <iostream>
|
|
//#include <vector>
|
|
//#include "ado.h"
|
|
|
|
#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>
|
|
using namespace std;
|
|
|
|
int bmain() {
|
|
ado ado0;
|
|
bool log = ado0.open("infodba", "infodba", "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=infodba;Initial Catalog=TC12;Data Source=DHRC");
|
|
if (log == false) {
|
|
cout << "登录成功" << endl;
|
|
char *sql = "select * from FTHZHJBM WHERE PREFIX='FCC20209ww9'";
|
|
//ado0.execute(sql);
|
|
|
|
}
|
|
else {
|
|
cout << "登录失败" << endl;
|
|
}
|
|
|
|
|
|
ado0.close();
|
|
return 0;
|
|
}
|
|
|
|
bool ado::open(char* UserName, char* Pwd, const char* CnnStr)
|
|
{
|
|
try
|
|
{
|
|
::CoInitialize(NULL); //初始化COM环境
|
|
m_pConnection.CreateInstance(__uuidof(Connection)); //创建连接对象
|
|
m_pConnection->Open(CnnStr, UserName, Pwd, adModeUnknown);
|
|
}
|
|
catch (_com_error e)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
bool ado::open(char* username, char* password, char* dbname, char* ip)
|
|
{
|
|
try
|
|
{
|
|
::CoInitialize(NULL); //初始化COM环境
|
|
m_pConnection.CreateInstance(__uuidof(Connection)); //创建连接对象
|
|
char connStr[200] = "";
|
|
sprintf_s(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 ado::execute(char *sqlStr)
|
|
{
|
|
|
|
|
|
cout << "完成执行" << endl;
|
|
try
|
|
{
|
|
m_pRecordSet = m_pConnection->Execute(sqlStr, NULL, 1);
|
|
cout << "插入成功" << endl;
|
|
//m_pRecordSet->Close();
|
|
/*if(m_pConnection->State){
|
|
m_pConnection->Close();
|
|
}*/
|
|
}
|
|
catch (_com_error e)
|
|
{
|
|
printf(e.Description());
|
|
return -1;
|
|
}
|
|
return false;
|
|
}
|
|
// 有输入参数的查询
|
|
int ado::ado_QuerySQL(char *SQL, int inputValueCount, char ** inputValue)
|
|
{
|
|
Fields * fields = NULL;
|
|
long ColCount = 0;
|
|
long RowCount = 0;
|
|
|
|
int 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;
|
|
|
|
outputValueCount = (int)RowCount;
|
|
}
|
|
|
|
if (outputValueCount <= 0)
|
|
{
|
|
return 0;
|
|
}
|
|
else {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
// 无输入参数的查询
|
|
int ado::ado_QuerySQLNoInputParam(char *SQL)
|
|
{
|
|
return ado_QuerySQL(SQL, 0, NULL);
|
|
}
|
|
|
|
bool ado::executeInsert(char* selectSql, char * insertSql,int result)
|
|
{
|
|
bool resultSql = false;
|
|
try
|
|
{
|
|
// m_pRecordSet = m_pConnection->Execute(selectSql, NULL, 1);
|
|
|
|
//_variant_t vPrefix, vFlow;
|
|
|
|
|
|
if (result==0) {
|
|
cout << "执行插入" << endl;
|
|
try {
|
|
cout << insertSql << endl;
|
|
m_pRecordSet = m_pConnection->Execute(insertSql, NULL, 1);
|
|
cout << "插入成功" << endl;
|
|
resultSql = true;
|
|
}
|
|
catch (_com_error e)
|
|
{
|
|
printf("error%s\n", e.Description());
|
|
resultSql = false;
|
|
}
|
|
}
|
|
m_pRecordSet->Close();
|
|
/*if(m_pConnection->State){
|
|
m_pConnection->Close();
|
|
}*/
|
|
return resultSql;
|
|
}
|
|
catch (_com_error)
|
|
{
|
|
//printf("error%s\n", e.Description() );
|
|
return false;
|
|
}
|
|
|
|
return resultSql;
|
|
}
|
|
bool ado::execute2(char* selectSql,char * updatesql,int result)
|
|
{
|
|
bool resultSql = false;
|
|
try
|
|
{
|
|
//m_pRecordSet = m_pConnection->Execute(selectSql, NULL, 1);
|
|
_variant_t vPrefix, vFlow;
|
|
|
|
// int result = ado_QuerySQLNoInputParam(selectSql);
|
|
//if (!m_pRecordSet->adoEOF) {
|
|
if (result == 1) {
|
|
cout << "执行更新" << endl;
|
|
try {
|
|
cout << updatesql << endl;
|
|
m_pRecordSet = m_pConnection->Execute(updatesql, NULL, 1);
|
|
cout << "更新成功" << endl;
|
|
resultSql = true;
|
|
}
|
|
catch (_com_error e)
|
|
{
|
|
printf("error%s\n", e.Description());
|
|
resultSql = false;
|
|
}
|
|
}
|
|
m_pRecordSet->Close();
|
|
/*if(m_pConnection->State){
|
|
m_pConnection->Close();
|
|
}*/
|
|
}
|
|
catch (_com_error)
|
|
{
|
|
//printf("error%s\n", e.Description() );
|
|
return false;
|
|
}
|
|
|
|
return resultSql;
|
|
}
|
|
bool ado::close()
|
|
{
|
|
try
|
|
{
|
|
m_pConnection->Close();
|
|
CoUninitialize();
|
|
cout << "退出登录" << endl;
|
|
}
|
|
catch (_com_error)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|