如何使用VC++程序在MySQL数据库中创建表? 都可以创建,创建表只是一句sql语句而已,ADO跟DOBC是一个类,无论那个类都是调用语句去执行。向多个表存数据也存入数据当然是可以的,用2个insert就OK了,中间用空格 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用ADO吧,把ADO当成app和数据库的中间层。连接好数据库之后,调用ADO的方法,然后写SQL语句。 哥们你的想法有点左了,你要建表最好用数据库管理软件来建立,一般每种数据库都带自己的管理软件。而不是用vc,当然vc也可以用sql语句实现,但总是怪怪的。 如果使用了ADO方法,那如何用程序实现在连接好的数据库里创建一个不存在的新表呢? 如果使用了ADO方法,那如何用程序实现在连接好的数据库里创建一个不存在的新表呢?SQL语句 CREATE TABLE ... 如果使用了ADO方法,那如何用程序实现在连接好的数据库里创建一个不存在的新表呢?SQL语句 CREATE TABLE ...这个语句不能直接使用,我试过了,到底该怎么用呢,还有我想按条件删除,也不会写啊,查了好多资料,没有找到合适的。 void CADO::OnInitADOConn( CString ServerID, CString dbName ){ // 初始化OLE/COM库环境 ::CoInitialize(NULL); CHAR FilePath[MAX_PATH]; memset(FilePath, 0x00, MAX_PATH); GetSyncPath(FilePath); TiXmlDocument* pDoc = new TiXmlDocument(FilePath); if (!(pDoc->LoadFile())) { ThrowBkException(FLUSH_MSG); } TiXmlElement* pRoot = pDoc->RootElement(); TiXmlElement* pDB = pRoot->FirstChildElement(XML_SYNC_SERVER); if (pDB == NULL) { AfxMessageBox("XML读取失败:Server"); } CString strServer = pDB->FirstChild()->Value(); pDB = pRoot->FirstChildElement(XML_SYNC_DB); if (pDB == NULL) { AfxMessageBox("XML读取失败:DB"); } CString strDB = pDB->FirstChild()->Value(); pDB = pRoot->FirstChildElement(XML_SYNC_USERID); if (pDB == NULL) { AfxMessageBox("XML读取失败:UserID"); } CString strUserID= pDB->FirstChild()->Value(); pDB = pRoot->FirstChildElement(XML_SYNC_PASSWORD); if (pDB == NULL) { AfxMessageBox("XML读取失败:password"); } CString strPassword = pDB->FirstChild()->Value(); try { // 创建Connection对象 m_pConnection.CreateInstance("ADODB.Connection"); // 设置连接字符串,必须是BSTR型或者_bstr_t类型 CString strConnect; strConnect.Format( "Driver={SQL Server};Server=%s;Uid=%s;Pwd=%s;Database=%s", strServer, strUserID, strPassword, strDB); m_pConnection->Open( _bstr_t(strConnect),"","",0);//adModeUnknown g_Log->TraceLog(LOG_LEVEL_DB, -1, "连接数据库", strConnect.GetBuffer(strConnect.GetLength())); } // 捕捉异常 catch(_com_error e) { // 显示错误信息 AfxMessageBox(e.Description()); } if(m_pConnection==NULL) MessageBox(NULL,"发生错误:\n\n","系统提示",MB_OK|MB_ICONEXCLAMATION);}_RecordsetPtr& CADO::GetRecordSet(CString strSQL){ _bstr_t bstrSQL; bstrSQL=strSQL; try { // 连接数据库,如果Connection对象为空,则重新连接数据库 if(m_pConnection==NULL) OnInitADOConn( "", "" ); // 创建记录集对象 m_pRecordset.CreateInstance(__uuidof(Recordset)); // 取得表中的记录 m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); g_Log->TraceLog(LOG_LEVEL_DB, -1, "GetRecordSet", strSQL.GetBuffer(strSQL.GetLength())); } // 捕捉异常 catch(_com_error e) { // 显示错误信息 AfxMessageBox(e.Description()); } // 返回记录集 return m_pRecordset;} // 执行SQL语句,Insert Update _variant_tBOOL CADO::ExecuteSQL(CString strSQL){ _bstr_t bstrSQL; bstrSQL=strSQL;// _variant_t RecordsAffected; try { // 是否已经连接数据库 if(m_pConnection == NULL) OnInitADOConn("", ""); // Connection对象的Execute方法:(_bstr_t CommandText, // VARIANT * RecordsAffected, long Options ) // 其中CommandText是命令字串,通常是SQL命令。 // 参数RecordsAffected是操作完成后所影响的行数, // 参数Options表示CommandText的类型:adCmdText-文本命令;adCmdTable-表名 // adCmdProc-存储过程;adCmdUnknown-未知 m_pConnection->Execute(bstrSQL,NULL,adCmdText); g_Log->TraceLog(LOG_LEVEL_DB, -1, "ExecuteSQL", strSQL.GetBuffer(strSQL.GetLength())); return true; } catch(_com_error e) { return false; }}void CADO::ExitConnect(){ // 关闭记录集和连接 if (m_pRecordset != NULL) m_pRecordset->Close(); m_pConnection->Close(); // 释放环境 ::CoUninitialize();} 上面那个只是查询,贴错了,贴个添加记录的,删除啥的同样调用ExecuteSQL(m_strSQL)执行SQL语句即可// 使用记录集打开int CTransBG::RecordSetOpen(CRecordset & recordSet, const CString & strSql){ if (!IsOpen()) { if (!ConnectODBC()) { return RET_ERR_SYSTEM; } } try { if (recordSet.IsOpen()) { recordSet.Close(); }// g_Log->TraceLog(LOG_LEVEL_NORMAL, 0, "recordSet.Open", strSql->GetBuffer(strSql->GetLength()));// strSql.ReleaseBuffer(strSql.GetLength()); if (!recordSet.Open(AFX_DB_USE_DEFAULT_TYPE, strSql, CRecordset::readOnly)) { g_Log->TraceLog(LOG_LEVEL_ERROR, 0, _T("执行数据库查询失败"), NULL); return RET_ERR_SYSTEM; } } catch (CMemoryException* e) { TCHAR szError[255]; memset(szError, 0x00, sizeof(szError)); e->GetErrorMessage(szError, sizeof(szError)); g_Log->TraceLog(LOG_LEVEL_ERROR, 0, _T("捕捉到内存异常"), szError); e->Delete(); return RET_ERR_SYSTEM; } catch (CDBException* e) { g_Log->TraceLog(LOG_LEVEL_ERROR, 0, _T("捕捉到数据库异常"), e->m_strError.GetBuffer( e->m_strError.GetLength())); e->Delete(); return RET_ERR_SYSTEM; } catch (CException* e) { TCHAR szError[255]; memset(szError, 0x00, sizeof(szError)); e->GetErrorMessage(szError, sizeof(szError)); g_Log->TraceLog(LOG_LEVEL_ERROR, 0, _T("捕捉到异常"), szError); e->Delete(); return RET_ERR_SYSTEM; } return RET_OK;}int CTransBG::SQLExecute(const CString & strSql){ if (!IsOpen()) { if (!ConnectODBC()) { return RET_ERR_SYSTEM; } } try { //g_logTrace.WriteLog(LOG_LEVEL_NORMAL, 0, "ExecuteSQL", strSql); ExecuteSQL(strSql); } catch (CMemoryException* e) { TCHAR szError[255]; memset(szError, 0x00, sizeof(szError)); e->GetErrorMessage(szError, sizeof(szError)); //g_logTrace.WriteLog(LOG_LEVEL_ERROR, 0, _T("捕捉到内存异常"), szError); e->Delete(); return RET_ERR_SYSTEM; } catch (CDBException* e) { //g_logTrace.WriteLog(LOG_LEVEL_ERROR, 0, _T("捕捉到数据库异常"), e->m_strError); e->Delete(); return RET_ERR_SYSTEM; } catch (CException* e) { TCHAR szError[255]; memset(szError, 0x00, sizeof(szError)); e->GetErrorMessage(szError, sizeof(szError)); //g_logTrace.WriteLog(LOG_LEVEL_ERROR, 0, _T("捕捉到异常"), szError); e->Delete(); return RET_ERR_SYSTEM; } return RET_OK;}int CTransBG::SaveSendBill(tagSendBill* pSendBill){ m_strSQL.Format("insert into %s (waybillNo,userName,recieveName,recieveAddr,PayTrans,transMoney" ",PayGoods,goodsMoney,signName,payMethod,sendTime)" " values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" , TABLE_SENDBILL, pSendBill->waybillNo,pSendBill->userName,pSendBill->recieveName ,pSendBill->recieveAddr,pSendBill->PayTrans,pSendBill->transMoney,pSendBill->PayGoods ,pSendBill->goodsMoney,pSendBill->signName,pSendBill->payMethod,pSendBill->sendTime); m_ado.OnInitADOConn("", ""); BOOL bDB = m_ado.ExecuteSQL(m_strSQL); if (bDB) { return DB_OK; } else { return DB_ERR; }} bool CDB::Exec(CString SQL){//::CoInitialize(NULL); try { _ConnectionPtr PConn("ADODB.Connection"); _variant_t RecordsAffected; CString LocalConnStr=((CServerApp*)AfxGetApp())->LocalConnStr; PConn->Open((_bstr_t)LocalConnStr,"","",adModeUnknown); if(PConn!=NULL) { PConn->Execute((_bstr_t)SQL,&RecordsAffected,adCmdText); } PConn->Close();//::CoUninitialize(); return true; } catch(_com_error e) //异常 {// CString Data;// Data.Format("Exec connect database error,error catagory is %s\n",(char *)e.ErrorMessage());// Debug(Data); } catch(...) { // Debug("Exec error!"); }//::CoUninitialize(); return false;}CDB::Exec("create table ....");CDB::Exec("insert into....");CDB::Exec("delete from ...."); 1,你要访问mysql数据库,必须通过SQL语句来访问,你要知道SQL语句的创建语句。2,在windows上可以安装mysql驱动(connector),并在ODBC中配置好连接信息。3,你要使用VC访问数据库,需要使用嵌入式SQL语句,可以通过ADO接口调用。 MS TV Video control控件如何使用 ADO连接MySql数据库.有连接时间限制么? 怎么为基于对话框的工具条上添加提示? 如何产生一个网页,并且可以添加一个表格,根据需求来设置表格内容 Matlab一启动,vc程序就出错!非常少见的错误。(分比较少,抱歉,容以后再加) vc忽略了异常规范,能让他不忽略吗? 为什么装了VC后,浏览网页时常出现"运行期错误",我用的是Win2000系统。 如何使用实现透明窗口? vb及vc的顶级高手请进!!! 关于输入法状态的问题 ffmpeg使用问题,纠结很久不知道原因,求大神 求大侠怎么做加速键MFC
这个语句不能直接使用,我试过了,到底该怎么用呢,还有我想按条件删除,也不会写啊,查了好多资料,没有找到合适的。
{
// 初始化OLE/COM库环境
::CoInitialize(NULL);
CHAR FilePath[MAX_PATH];
memset(FilePath, 0x00, MAX_PATH);
GetSyncPath(FilePath); TiXmlDocument* pDoc = new TiXmlDocument(FilePath);
if (!(pDoc->LoadFile()))
{
ThrowBkException(FLUSH_MSG);
} TiXmlElement* pRoot = pDoc->RootElement();
TiXmlElement* pDB = pRoot->FirstChildElement(XML_SYNC_SERVER);
if (pDB == NULL)
{
AfxMessageBox("XML读取失败:Server");
}
CString strServer = pDB->FirstChild()->Value();
pDB = pRoot->FirstChildElement(XML_SYNC_DB);
if (pDB == NULL)
{
AfxMessageBox("XML读取失败:DB");
}
CString strDB = pDB->FirstChild()->Value();
pDB = pRoot->FirstChildElement(XML_SYNC_USERID);
if (pDB == NULL)
{
AfxMessageBox("XML读取失败:UserID");
}
CString strUserID= pDB->FirstChild()->Value();
pDB = pRoot->FirstChildElement(XML_SYNC_PASSWORD);
if (pDB == NULL)
{
AfxMessageBox("XML读取失败:password");
}
CString strPassword = pDB->FirstChild()->Value();
try
{
// 创建Connection对象
m_pConnection.CreateInstance("ADODB.Connection");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
CString strConnect;
strConnect.Format( "Driver={SQL Server};Server=%s;Uid=%s;Pwd=%s;Database=%s", strServer, strUserID, strPassword, strDB);
m_pConnection->Open( _bstr_t(strConnect),"","",0);//adModeUnknown
g_Log->TraceLog(LOG_LEVEL_DB, -1, "连接数据库", strConnect.GetBuffer(strConnect.GetLength()));
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(e.Description());
}
if(m_pConnection==NULL)
MessageBox(NULL,"发生错误:\n\n","系统提示",MB_OK|MB_ICONEXCLAMATION);
}
_RecordsetPtr& CADO::GetRecordSet(CString strSQL)
{
_bstr_t bstrSQL;
bstrSQL=strSQL;
try
{
// 连接数据库,如果Connection对象为空,则重新连接数据库
if(m_pConnection==NULL)
OnInitADOConn( "", "" );
// 创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
g_Log->TraceLog(LOG_LEVEL_DB, -1, "GetRecordSet", strSQL.GetBuffer(strSQL.GetLength()));
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(e.Description());
}
// 返回记录集
return m_pRecordset;
} // 执行SQL语句,Insert Update _variant_t
BOOL CADO::ExecuteSQL(CString strSQL)
{
_bstr_t bstrSQL;
bstrSQL=strSQL;
// _variant_t RecordsAffected;
try
{
// 是否已经连接数据库
if(m_pConnection == NULL)
OnInitADOConn("", "");
// Connection对象的Execute方法:(_bstr_t CommandText,
// VARIANT * RecordsAffected, long Options )
// 其中CommandText是命令字串,通常是SQL命令。
// 参数RecordsAffected是操作完成后所影响的行数,
// 参数Options表示CommandText的类型:adCmdText-文本命令;adCmdTable-表名
// adCmdProc-存储过程;adCmdUnknown-未知
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
g_Log->TraceLog(LOG_LEVEL_DB, -1, "ExecuteSQL", strSQL.GetBuffer(strSQL.GetLength()));
return true;
}
catch(_com_error e)
{
return false;
}
}void CADO::ExitConnect()
{
// 关闭记录集和连接
if (m_pRecordset != NULL)
m_pRecordset->Close();
m_pConnection->Close();
// 释放环境
::CoUninitialize();
}
// 使用记录集打开
int CTransBG::RecordSetOpen(CRecordset & recordSet, const CString & strSql)
{
if (!IsOpen())
{
if (!ConnectODBC())
{
return RET_ERR_SYSTEM;
}
} try
{
if (recordSet.IsOpen())
{
recordSet.Close();
}// g_Log->TraceLog(LOG_LEVEL_NORMAL, 0, "recordSet.Open", strSql->GetBuffer(strSql->GetLength()));
// strSql.ReleaseBuffer(strSql.GetLength());
if (!recordSet.Open(AFX_DB_USE_DEFAULT_TYPE, strSql, CRecordset::readOnly))
{
g_Log->TraceLog(LOG_LEVEL_ERROR, 0, _T("执行数据库查询失败"), NULL);
return RET_ERR_SYSTEM;
}
}
catch (CMemoryException* e)
{
TCHAR szError[255];
memset(szError, 0x00, sizeof(szError));
e->GetErrorMessage(szError, sizeof(szError));
g_Log->TraceLog(LOG_LEVEL_ERROR, 0, _T("捕捉到内存异常"), szError);
e->Delete();
return RET_ERR_SYSTEM;
}
catch (CDBException* e)
{
g_Log->TraceLog(LOG_LEVEL_ERROR, 0, _T("捕捉到数据库异常"), e->m_strError.GetBuffer( e->m_strError.GetLength()));
e->Delete();
return RET_ERR_SYSTEM;
}
catch (CException* e)
{
TCHAR szError[255];
memset(szError, 0x00, sizeof(szError));
e->GetErrorMessage(szError, sizeof(szError));
g_Log->TraceLog(LOG_LEVEL_ERROR, 0, _T("捕捉到异常"), szError);
e->Delete();
return RET_ERR_SYSTEM;
}
return RET_OK;
}int CTransBG::SQLExecute(const CString & strSql)
{
if (!IsOpen())
{
if (!ConnectODBC())
{
return RET_ERR_SYSTEM;
}
} try
{
//g_logTrace.WriteLog(LOG_LEVEL_NORMAL, 0, "ExecuteSQL", strSql);
ExecuteSQL(strSql);
}
catch (CMemoryException* e)
{
TCHAR szError[255];
memset(szError, 0x00, sizeof(szError));
e->GetErrorMessage(szError, sizeof(szError));
//g_logTrace.WriteLog(LOG_LEVEL_ERROR, 0, _T("捕捉到内存异常"), szError);
e->Delete();
return RET_ERR_SYSTEM;
}
catch (CDBException* e)
{
//g_logTrace.WriteLog(LOG_LEVEL_ERROR, 0, _T("捕捉到数据库异常"), e->m_strError);
e->Delete();
return RET_ERR_SYSTEM;
}
catch (CException* e)
{
TCHAR szError[255];
memset(szError, 0x00, sizeof(szError));
e->GetErrorMessage(szError, sizeof(szError));
//g_logTrace.WriteLog(LOG_LEVEL_ERROR, 0, _T("捕捉到异常"), szError);
e->Delete();
return RET_ERR_SYSTEM;
}
return RET_OK;
}
int CTransBG::SaveSendBill(tagSendBill* pSendBill)
{
m_strSQL.Format("insert into %s (waybillNo,userName,recieveName,recieveAddr,PayTrans,transMoney"
",PayGoods,goodsMoney,signName,payMethod,sendTime)"
" values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"
, TABLE_SENDBILL, pSendBill->waybillNo,pSendBill->userName,pSendBill->recieveName
,pSendBill->recieveAddr,pSendBill->PayTrans,pSendBill->transMoney,pSendBill->PayGoods
,pSendBill->goodsMoney,pSendBill->signName,pSendBill->payMethod,pSendBill->sendTime);
m_ado.OnInitADOConn("", "");
BOOL bDB = m_ado.ExecuteSQL(m_strSQL);
if (bDB)
{
return DB_OK;
}
else
{
return DB_ERR;
}
}
{
//::CoInitialize(NULL); try
{
_ConnectionPtr PConn("ADODB.Connection");
_variant_t RecordsAffected;
CString LocalConnStr=((CServerApp*)AfxGetApp())->LocalConnStr;
PConn->Open((_bstr_t)LocalConnStr,"","",adModeUnknown);
if(PConn!=NULL)
{
PConn->Execute((_bstr_t)SQL,&RecordsAffected,adCmdText);
}
PConn->Close();
//::CoUninitialize();
return true;
}
catch(_com_error e) //异常
{
// CString Data;
// Data.Format("Exec connect database error,error catagory is %s\n",(char *)e.ErrorMessage());
// Debug(Data);
}
catch(...)
{
// Debug("Exec error!");
}
//::CoUninitialize();
return false;
}
CDB::Exec("create table ....");
CDB::Exec("insert into....");
CDB::Exec("delete from ....");
2,在windows上可以安装mysql驱动(connector),并在ODBC中配置好连接信息。
3,你要使用VC访问数据库,需要使用嵌入式SQL语句,可以通过ADO接口调用。