这是我写的程序里面关于数据库管理的程序,用的是ADO方式访问SQLServer
ConnectionPtr CAdoDatabase::Connect(CString strCon) //数据库连接,strCon为数据库名
{
CString str;
str.Format("Provider = SQLOLEDB.1;Password = ok;Persist Security Info = True;User ID = sa;Initial Catalog = %s",strCon);
pConn->ConnectionString = _bstr_t(str);
pConn->Open("", "", "", adConnectUnspecified);
return pConn;
}
//SQL语句的执行strCon为数据库名,strText为SQL语句字符串
_RecordsetPtr CAdoDatabase::Execute(CString strText, CString strCon)
{
if(pCmd->ActiveConnection == NULL)
pCmd->ActiveConnection = Connect(strCon);
pCmd->CommandText = _bstr_t(strText);
pRst = pCmd->Execute(NULL, NULL, adCmdText);
return pRst;
}
比如
CString str;
str.Format("insert into
recv values('%s', '%s', '%s', %d)",
sender_code, sms_contant, sms_date, sms_stor_id);
//recv为数据库的表名
Execute(str);