?? sql_connect1.cpp
字號:
// sql_connect1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//9月15日
//C++連接SQL數(shù)據(jù)庫
void smsLogon::InitSQLServer(CString server, CString db, CString UserName, CString Pwd)
{
m_pConnection.CreateInstance("ADODB.Connection");
CString strCn;
strCn.Empty();
strCn="provider=SQLOLEDB;data source="+server
+";initial catalog="+db
+";userID="+UserName
+";Password="+Pwd;
_variant_t bcnstr=_variant_t(strCn);
_variant_t bunstr=_variant_t(UserName);
_variant_t bpwdstr=_variant_t(Pwd);
//打開一個連接
try
{
m_pConnection->Open(_bstr_t(bcnstr),_bstr_t(bunstr),
_bstr_t(bpwdstr),-1);//adOpenUnspecified
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("Warning: 連接數(shù)據(jù)庫發(fā)生異常. 錯誤信息: %s; 文件: %s; 行: %d\n", e.ErrorMessage(), __FILE__, __LINE__);
AfxMessageBox(errormessage);
}
catch(...)
{
AfxMessageBox("Warning :連接數(shù)據(jù)庫時發(fā)生未知錯誤");
}
}
void smsLogon::ExitDB() //退出時關(guān)閉數(shù)據(jù)庫連接
{
if(m_pConnection!=NULL)
{
m_pConnection->Close();//關(guān)閉連接
m_pConnection.Release();//釋放對象
}
}
int main(int argc, char* argv[])
{
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -