Rumah >pangkalan data >tutorial mysql >ADO方式操作ACCESS数据库
欢迎进入C/C++编程社区论坛,与200万技术人员互动交流 >>进入 (1)通过JET数据库引擎对ACCESS2000数据库的连接: m_pConnection-Open(\"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\\\test.mdb\",\"\",\"\",adModeUnknown); (2)通过DSN数据源对任
欢迎进入C/C++编程社区论坛,与200万技术人员互动交流 >>进入
m_pConnection->Open(\"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\\\test.mdb\",\"\",\"\",adModeUnknown); |
m_pConnection->Open(\"Data Source=adotest;UID=sa;PWD=;\",\"\",\"\",adModeUnknown); |
m_pConnection->Open(\"driver={SQL Server};Server=127.0.0.1;DATABASE=vckbase; UID=sa;PWD=139\",\"\",\"\",adModeUnknown); |
m_pConnection->ConnectionTimeout = 5;///设置超时时间为5秒 m_pConnection->Open(\"Data Source=adotest;\",\"\",\"\",adModeUnknown); |
State属性指明当前Connection对象的状态,0表示关闭,1表示已经打开,我们可以通过读取这个属性来作相应的处理,例如:
if(m_pConnection->State) m_pConnection->Close(); ///如果已经打开了连接则关闭它 |
_RecordsetPtr Connection15::Execute ( _bstr_t CommandText, VARIANT * RecordsAffected, long Options ) |
_variant_t RecordsAffected; ///执行SQL命令:CREATE TABLE创建表格users,users包含四个字段:整形ID,字符串username,整形old,日期型birthday m_pConnection->Execute(\"CREATE TABLE users(ID INTEGER,username TEXT,old INTEGER,birthday DATETIME)\",&RecordsAffected,adCmdText); ///往表格里面添加记录 m_pConnection->Execute(\"INSERT INTO users(ID,username,old,birthday) VALUES (1, \'Washington\',25,\'1970/1/1\')\",&RecordsAffected,adCmdText); ///将所有记录old字段的值加一 m_pConnection->Execute(\"UPDATE users SET old = old+1\",&RecordsAffected,adCmdText); ///执行SQL统计命令得到包含记录条数的记录集 m_pRecordset = m_pConnection->Execute(\"SELECT COUNT(*) FROM users\",&RecordsAffected,adCmdText); _variant_t vIndex = (long)0; _variant_t vCount = m_pRecordset->GetCollect(vIndex);///取得第一个字段的值放入vCount变量 m_pRecordset->Close();///关闭记录集 CString message; message.Format(\"共有%d条记录\",vCount.lVal); AfxMessageBox(message);///显示当前记录条数 |
_CommandPtr m_pCommand; m_pCommand.CreateInstance(\"ADODB.Command\"); _variant_t vNULL; vNULL.vt = VT_ERROR; vNULL.scode = DISP_E_PARAMNOTFOUND;///定义为无参数 m_pCommand->ActiveConnection = m_pConnection;///非常关键的一句,将建立的连接赋值给它 m_pCommand->CommandText = \"SELECT * FROM users\";///命令字串 m_pRecordset = m_pCommand->Execute(&vNULL,&vNULL,adCmdText); //执行命令取得记录集 |
[1] [2] [3]