_RecordsetPtr pRst(__uuidof(Recordset));
CString strServer = "Localhost"; //本機服務器名稱
CString strDBFile = "xscj"; //數據庫名
CString strConn; //連接字符串
strConn.Format(_T("Driver=MySQL ODBC 3.51 Driver;;charset=gbk;Server=%s;Database=%s;
UID=root;PWD=root"),strServer,strDBFile);
pConn->Open ((_bstr_t)strConn,"","",-1);
pRst=pConn->Execute("select * from xs",NULL,adCmdText);
while(!pRst->rsEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)pRst->GetCollect("XM"));
pRst->MoveNext ();
}
首先要保證數據選用的編碼格式爲GBK(這是許多前輩們建議的,這個字符集支持的比較多),而且在MySql中還可以設置表的,字段的編碼格式。
其次,在ADO連接代碼中做必要的設置,具體設置如下:
try
{
//設置ADO使用的字符集
myConn->Execute("set character_set_connection=gbk",NULL,adCmdText);
myConn->Execute("set character_set_results=gbk",NULL,adCmdText);
myConn->Execute("set character_set_client=gbk",NULL,adCmdText);
myConn->Execute(_bstr_t(sqlText),NULL,adCmdText);
}
catch (_com_error e)
{
msg = e.ErrorMessage();
}