void CExampleDlg::OnBnClickedConnectButton() { WCHAR awcTempBuf[100]; const int kTempBufSize = sizeof(awcTempBuf) / sizeof(awcTempBuf[0]); // Pull user input into our member variables UpdateData(TRUE); // Clear out the results list, in case this isn't the first time // we've come in here. ResultsList.ResetContent(); // Translate the Unicode text we get from the UI into the UTF-8 form // that MySQL wants. const int kInputBufSize = 100; char acServerAddress[kInputBufSize]; char acUserName[kInputBufSize]; char acPassword[kInputBufSize]; ToUTF8(acServerAddress, kInputBufSize, sServerAddress); ToUTF8(acUserName, kInputBufSize, sUserName); ToUTF8(acPassword, kInputBufSize, sPassword); // Connect to the sample database. mysqlpp::Connection con(false); if (!con.connect("mysql_cpp_data", acServerAddress, acUserName, acPassword)) { AddMessage(_T("Failed to connect to server:")); if (ToUCS2(awcTempBuf, kTempBufSize, con.error())) { AddMessage(awcTempBuf); } return; } // Retrieve a subset of the sample stock table set up by resetdb mysqlpp::Query query = con.query(); query << "select item from stock"; mysqlpp::StoreQueryResult res = query.store(); if (res) { // Display the result set for (size_t i = 0; i < res.num_rows(); ++i) { if (ToUCS2(awcTempBuf, kTempBufSize, res[i][0])) { AddMessage(awcTempBuf); } } // Retreive was successful, so save user inputs now SaveInputs(); } else { // Retreive failed AddMessage(_T("Failed to get item list:")); if (ToUCS2(awcTempBuf, kTempBufSize, query.error())) { AddMessage(awcTempBuf); } } }
__inline bool ToUTF8(MinimalStringT<char> &dest, LPCSTR source) { MinimalStringT<wchar_t> unistr(dest.GetAllocator()); if (!ToUCS2(unistr, source)) return false; int outLen = ::WideCharToMultiByte( CP_UTF8, 0, unistr, unistr.GetSize(), NULL, 0, NULL, NULL); if (outLen == 0) return false; dest.Grow(outLen + 1); int convLen = ::WideCharToMultiByte( CP_UTF8, 0, unistr, unistr.GetSize(), dest, outLen, NULL, NULL); if (outLen != convLen) return false; dest.GetRaw()[outLen] = 0; dest.Repair(); return true; }
MinimalA2W(const char *source) { ToUCS2(this->s, source); }
Json::WString Json::wstring() const { return ToUCS2(string()); }