vc+FTP:
1.stdafx.h添加
//FTP
#include < afxinet.h >
void DisplayMsg(LPCTSTR xi_cszFormat, ...);
2.dlg.h添加
//FTP
CInternetSession *m_pInetSession;
CFtpConnection *m_pFtpConnection;
3.连接
// 连接 ftp
CString m_HostEdit=_T("192.168.25.131");
CString m_UserEdit=_T("xx");
CString m_PasswordEdit=_T("'");
m_pInetSession = new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
try
{
m_pFtpConnection = m_pInetSession->GetFtpConnection(m_HostEdit, m_UserEdit, m_PasswordEdit);
//AfxMessageBox(_T("连接到 " )+ m_HostEdit + _T("成功!"));
}
catch(CInternetException *pEx)
{
TCHAR szError[1024];
if(pEx->GetErrorMessage(szError,1024))
AfxMessageBox(szError);
else
AfxMessageBox(_T("发生错误."));
pEx->Delete();
m_pFtpConnection=NULL;
}
4.传输
if (!pDlg->m_pFtpConnection->PutFile(strDir+_T("upload"), _T("upload")))
{
//上传失败
DisplayMsg(_T("上传 upload 出错"));
}
if (!pDlg->m_pFtpConnection->GetFile(strBFileName, strDir+strBFileName))
{
//下载失败
//DisplayMsg(_T("%s\n下载失败"),strBFileName);
//break;
}
vc+SQLServer:
1.stdafx.h添加
//1、添加对ADO的支持
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
APP::InitInstance()添加: CoInitialize(NULL);
App::ExitInstance()添加: CoUninitialize();
2.三个对象:
_ConnectionPtr m_pConnection; // 数据库
_RecordsetPtr m_pRecordset; // 命令
_CommandPtr m_pCommand; // 记录
3.连接数据库:
HRESULT hr = NULL;
try
{
hr = m_pConnection.CreateInstance(__uuidof(Connection));//创建连接对象实例
if (SUCCEEDED(hr))
{
m_pConnection->ConnectionString =_T("Provider=SQLOLEDB.1; server=192.168.25.131;database=shujuguanli; Persist Security Info=False; UID=sa; PWD=x;");
hr = m_pConnection->Open("", "", "", adConnectUnspecified);//打开数据库
if (FAILED(hr))
{
AfxMessageBox(_T("Open Failed!"));
}
}
else
{
AfxMessageBox(_T("Create instance of connection failed!"));
}
}
catch (_com_error e)
{
CString temp;
temp.Format(_T("数据库连接错误\r\n错误信息为:%s"), e.ErrorMessage());
AfxMessageBox(temp);
}
4.数据访问:
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open(_T("insert into [user](name) values(\'xxx\')"),
_variant_t((IDispatch*)m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText);
}
catch (_com_error &e)
{
AfxMessageBox(e.Description());
}
_variant_t sno, cno, grade;
try
{
//int num = m_list.GetItemCount();
while (!m_pRecordset->adoEOF)
{
CString sno= (_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("id"))->Value);
CString cno = (_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("name"))->Value);
DisplayMsg(_T("%s %s"), sno,cno);
m_pRecordset->MoveNext();
}
}
catch (_com_error &e)
{
AfxMessageBox(e.Description());
}