加我Q 给你一个类库 增删改查 523740321
---篇幅太小贴不全啊
// Ado.cpp: interface for the CAdo class.
//制作人 韩琦
//程序版本:1.0
#include "stdafx.h"
#include "ADO.h"
#include "stdio.h"
//添加字符串处理
#include
//添加10进制16进制转换
#include
ADO::ADO(void)
{
}
ADO::~ADO(void)
{
}
////数据库初始设置
//对数据库进行连接
/*
输入条件 无
输出结果 无
*/
void ADO::InitADOConn()
{
CoInitialize(NULL);
pConn.CreateInstance (__uuidof(Connection));
pRst.CreateInstance (__uuidof(Recordset));
_bstr_t strConn;
//strConn="Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;User ID=sa;Initial Catalog=INDV;Data Source=.";
//orgin connection
strConn="Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;User ID=sa;Initial Catalog=INDV;Data Source=软件管理服务器\\CW_DATA";
//增加错误陷阱
try
{
pConn->ConnectionString=strConn;
pConn->Open (strConn,"sa","123456",adConnectUnspecified);
}
catch(_com_error e)
{
AfxMessageBox("数据库无法连接!");
}
}
////连接关闭操作
//进行连接关闭的操作
/*
输入条件为 无
输出结果为 无
*/
void ADO::CloseConn()
{
try
{
if(pConn->State)//不能多次关闭,否则会出现错误
pConn->Close();
CoUninitialize();
}
catch(_com_error e)
{
AfxMessageBox("数据库无法连接!");
}
}
////执行SQL语句
//执行SQL语句
/*
输入条件为 SQL语句
sql
输出结果为 满足条件的结果集
*/
_RecordsetPtr& ADO::Execute(CString sql)
{
try
{
pRst=pConn->Execute (sql.GetBuffer(),NULL,adCmdText);
}
catch(_com_error e)
{
AfxMessageBox("执行SQL语句错误!");
}
return pRst;
}
//执行存储过程的函数 ExecStore
/*
输入条件为 查询条件 查询页数 获得的结果集
sql
输出结果为 True 成功 False 失败
*/
bool ADO::ExecStore( CString m_Where,int m_PageNum,_RecordsetPtr &m_GetRst)
{
std::string strProcName;
// 拼装条件
CString m_ls;
m_ls="ps_PageView";
CString m_ls1=" 'ps_contours','ps_num',4,20,'*','PS_CREDATE','ps_num<>''1''' ";
m_ls+=m_ls1;
_CommandPtr pCommand;
int iExpVal;
try {
pCommand.CreateInstance(__uuidof(Command));
pCommand->ActiveConnection =pConn;//连接字符串
pCommand->CommandType = adCmdStoredProc;//标记该操作为存储过程
_variant_t vt;
vt.SetString("2");
//加入各个参数()
_ParameterPtr pParam, pParam1;
pParam.CreateInstance(__uuidof(Parameter));
_bstr_t storename("ps_PageView");
pCommand->Parameters->Append(pCommand->CreateParameter("@@FieldKey", adVarChar, adParamInput,20, "PS_NUM"));
pCommand->Parameters->Append(pCommand->CreateParameter("@PageCurrent", adInteger, adParamInput, -1, m_PageNum));
pCommand->Parameters->Append(pCommand->CreateParameter("@PageSize", adInteger, adParamInput, -1, "20"));
//2011-8-30修改加入PS_ID项目的输出
pCommand->Parameters->Append(pCommand->CreateParameter("@FieldShow", adVarChar, adParamInput, 200, "PS_ID,PS_NUM,IPC_NUM,IPCAM_NUM,CLSA_CONTOURS,CLSB_CONTOURS,CLSC_CONTOURS,CLSD_CONTOURS,PS_CREDATE,PS_REMARK"));
pCommand->Parameters->Append(pCommand->CreateParameter("@FieldOrder", adVarChar, adParamInput, 50, " PS_ID"));
pCommand->Parameters->Append(pCommand->CreateParameter("@Where", adVarChar, adParamInput, 200, (_variant_t)m_Where) );
int b=0;
pCommand->Parameters->Append(pCommand->CreateParameter("@PageCount", adInteger, adParamInputOutput ,-1,b));
//pCommand->Parameters->Append(pCommand->CreateParameter("@return_value", adInteger, adParamReturnValue ,-1,rst));
pCommand->put_CommandText( storename );
//int b=0;
//pCommand->Parameters->Append(pCommand->CreateParameter("@PageCount", adInteger, adParamInputOutput ,-1,b));
m_GetRst=pCommand->Execute(NULL,NULL,adCmdStoredProc);
//pCommand->Parameters->Append(pCommand->CreateParameter("@RETURN_VALUE", adInteger, adParamReturnValue ,-1,rst));
// pCommand->Parameters->Append(pCommand->CreateParameter("@PageCount", adInteger, adParamOutput ,-1,b));
}
catch (_com_error e)
{
CString m_strErrMsg;
m_strErrMsg.Format("LogUserSearchKey failure!\r\n\r\n message error:%s\r\n\r\n", e.ErrorMessage());
printf("%s\n", m_strErrMsg.GetBuffer());
// IsDatabaseClose();
iExpVal = 0;
return false;
}
return true;
}
}
if ( sizeof(m_Ipcamnum)<1 )
{
AfxMessageBox("输入的工控机编号位数太短无效!");
return false;
}
////拼装WHERE语句
//-------------------------------------------------------------------------------------
CString m_SqlWhere=" 1=1 ";
//CString m_SqlWhere=" WHERE 1=1 ";
CString m_Ls;
//拼装工控机编号
if (m_Ipcnum!="全部")
{
m_Ls=" AND IPC_NUM= '"+m_Ipcnum+"'";
m_SqlWhere+=m_Ls;
}
//拼装相机编号
if (m_Ipcamnum!="全部")
{
m_Ls=" AND IPCAM_NUM='"+m_Ipcamnum+"'";
m_SqlWhere+=m_Ls;
}
//拼装时间范围
m_Ls=" AND PS_CREDATE BETWEEN '"+m_StartTime+"' AND '" +m_EndTime+"' ";
m_SqlWhere+=m_Ls;
//拼装等级
m_Ls=" AND 1=1 ";
m_SqlWhere+=m_Ls;
//执行查询语句
int send=0;
CString SQL="";
SQL.Format("SELECT COUNT(*) FROM PS_CONTOURS WHERE %s" ,m_SqlWhere);
try
{
_RecordsetPtr bb=ADO::Execute(SQL);
_variant_t vIndex = (long)0;
_variant_t vCount = pRst->GetCollect(vIndex);///取得第一个字段的值放入vCount变量
send= (int) vCount.llVal ;
if (send%20)
send=send/20;
else
send=send/20+1;
if (send<20)
send=1;
bb.Release();
}
catch(_com_error e)
{
AfxMessageBox("记录集无法使用!");
return 0;
}
return send;
}
//获得数据库中瑕疵的总数
/*
输入条件为 起始时间 结束时间 PS等级
StartDate EndDate Get_PsLevel
输出结果为 满足条件的PS板数量
*/
bool ADO::GetsqlSum( CString StartDate,CString EndDate,int (&get)[4] )
{
//输入条件判断
if ( StartDate.StringLength(StartDate)<10 )
{
AfxMessageBox("输入的开始时间错误!");
return false;
}
if ( EndDate.StringLength(EndDate)<10 )
{
AfxMessageBox("输入的结束时间错误!");
return false;
}
//执行查询语句
try
{
CString SQL="";
SQL.Format("SELECT 1 AS CLSLEVEL,ISNULL( SUM(CLSA_CONTOURS),0) AS SUMLEVEL FROM PS_CONTOURS WHERE PS_CREDATE BETWEEN '%s' AND '%s' UNION SELECT 2 AS CLSLEVEL,ISNULL( SUM(CLSB_CONTOURS),0) AS SUMLEVEL FROM PS_CONTOURS WHERE PS_CREDATE BETWEEN '%s' AND '%s' UNION SELECT 3 AS CLSLEVEL,ISNULL( SUM(CLSA_CONTOURS),0) AS SUMLEVEL FROM PS_CONTOURS WHERE PS_CREDATE BETWEEN '%s' AND '%s' UNION SELECT 4 AS CLSLEVEL,ISNULL( SUM(CLSA_CONTOURS),0) AS SUMLEVEL FROM PS_CONTOURS WHERE PS_CREDATE BETWEEN '%s' AND '%s' " ,StartDate,EndDate,StartDate,EndDate,StartDate,EndDate,StartDate,EndDate);
_RecordsetPtr rst=ADO::Execute(SQL);
//}
//逐一取值
_variant_t GetRst=rst->GetCollect("CLSLEVEL");
get[0]=GetRst.llVal ;
rst->MoveNext ();
GetRst=rst->GetCollect("CLSLEVEL");
get[1]=GetRst.llVal ;
rst->MoveNext ();
GetRst=rst->GetCollect("CLSLEVEL");
get[2]=GetRst.llVal ;
rst->MoveNext ();
GetRst=rst->GetCollect("CLSLEVEL");
get[3]=GetRst.llVal ;
return true;
}
catch(_com_error e)
{
AfxMessageBox("记录集无法使用!");
return false;
}
}
进控制面板里,odbc连接,设置你的数据库