VC下利用ADO访问Access数据库(Use ADO)(转载)
VC下利用ADO直接访问Access数据库步骤不需要用户建立ODBC数据源) 1.包含相关动态链接库 //在StdAfx.h中,最后部分添加(注意:一定要在最后部分,否则会编译出错) #import c:/Program Files/Common Files/System/ado/msado15.dll no_namespace rename(EOF,adoEO
VC下利用ADO直接访问Access数据库步骤不需要用户建立ODBC数据源)
1.包含相关动态链接库
-
//在StdAfx.h中,最后部分添加(注意:一定要在最后部分,否则会编译出错)
-
#import "c:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF","adoEOF")
2.连接的创建与初始化
-
//相关成员变量
-
_ConnectionPtr m_conn;
-
_RecordsetPtr m_res;
-
-
//成员函数块(一般写在CDocment类构造函数即可)
-
try
-
{
-
CoInitialize(NULL);
-
m_conn.CreateInstance(_uuidof(Connection));
-
CString strFileName;
-
strFileName = "MYBASE.mdb"; //添加相应你的数据库的文件名,编辑状态应放在源文件目录下
-
m_conn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFileName,
-
"","",adConnectUnspecified); //用户名,密码
-
m_res.CreateInstance(_uuidof(Recordset));
-
}
-
catch(_com_error e) //异常检测
-
{
-
AfxMessageBox("数据库连接错误!",MB_ICONEXCLAMATION);
-
exit(0); //错误,程序退出
-
}
3.数据库相关操作(操作方法很多,这里只提供一种简易操作)
假设数据库表设计如下:
表名: MYTABLE
表设计:
自动编号类型 ID
字符串类型 NAME
BOOL类型 SEX
(1)增
-
_variant_t m_resa; //可声明为成员变量
-
-
CString strMyName = "MyName";
-
CString strSex = "true";
-
-
CString sql;
-
sql = "insert into MYTABLE (NAME,SEX) ";
-
sql += "values ('" + strMyName +"',";
-
sql += " " + strSex + " ";
-
sql += ")";
-
-
try
-
{
-
m_conn->Execute((_bstr_t)sql,&m_resa,adCmdText); //执行"增"操作
-
}
-
catch(_com_error e)
-
{
-
AfxMessageBox("数据库增错误",MB_ICONEXCLAMATION);
-
exit(0); //错误,程序退出
-
}
(2)删
-
_variant_t m_resa; //可声明为成员变量
-
-
CString strID = "1"; //所要删除记录的ID号
-
CString sql;
-
-
sql = "delete from MYTABLE "; //注意需要有空格
-
sql += "where ID = " + strID; //其他语法详查SQL语句
-
-
try
-
{
-
m_conn->Execute((_bstr_t)sql,&m_resa,adCmdText); //执行"增"操作
-
}
-
catch(_com_error e)
-
{
-
AfxMessageBox("数据库删错误",MB_ICONEXCLAMATION);
-
exit(0); //错误,程序退出
-
}
(3)改
-
_variant_t m_resa; //可声明为成员变量
-
-
CString sql;
-
CString strMyName = "MyName";
-
CString strSex = "true";
-
CString strID = "1"; //所要更新的记录ID
-
-
sql = "update MYTABLE set ";
-
sql += "NAME = '" + strMyName +"', ";
-
sql += "SEX = " + strSex + " ";
-
sql += "where id = " + strID;
-
-
try
-
{
-
m_conn->Execute((_bstr_t)sql,&m_resa,adCmdText); //执行"增"操作
-
}
-
catch(_com_error e)
-
{
-
AfxMessageBox("数据库删错误",MB_ICONEXCLAMATION);
-
exit(0); //错误,程序退出
-
}
(4)查
-
_variant_t m_resa; //可声明为成员变量
-
CString sql;
-
sql = "Select * from MYTABLE"; //查询语句改变,相应下面的语句也要改变
-
-
//#include
//需加入头文件 -
//CArray
m_Array; //可用容器保存你取得的数据 -
-
try
-
{
-
m_res = m_conn->Execute((_bstr_t)sql,&m_resa,adCmdText);
-
}
-
catch(_com_error e)
-
{
-
AfxMessageBox("数据库查错误",MB_ICONEXCLAMATION);
-
exit(0);
-
}
-
-
//m_Array.RemoveAll(); //清空容器
-
-
try
-
{
-
while(!m_res->adoEOF) //循环遍历记录
-
{
-
_variant_t vID, vName, vSex;
-
-
vID = m_res->GetCollect("ID");
-
vName = m_res->GetCollect("NAME");
-
vSex = m_res->GetCollect("SEX");
-
-
///////////////////////////////////////////////
-
int nID;
-
nID = (long)vID.lVal;
-
///////////////////////////////////////////////
-
CString strName;
-
if(VT_NULL != vName.vt ) //如果数据不为空
-
{
-
strName = (LPCTSTR)vName.bstrVal;
-
}
-
///////////////////////////////////////////////
-
bool bSex;
-
if(VT_NULL != vSex.vt )
-
{
-
bSex = (bool)vSex.boolVal;
-
}
-
////////////////////////////////////////////////
-
-
//CMyClass one(nID, strName, bSex); //创建数据对象
-
//m_Array.Add(one); //加入数组
-
-
m_res->MoveNext(); //移动到下一条记录
-
}
-
}
-
catch(_com_error e)
-
{
-
AfxMessageBox("数据库查错误",MB_ICONEXCLAMATION);
-
exit(0);
-
}
4.关闭数据库
-
try
-
{
-
if(m_res != NULL)
-
{
-
m_res->Close(); //关闭记录集
-
}
-
if(m_conn != NULL)
-
{
-
m_conn->Close(); //关闭连接
-
}
-
}
-
catch(_com_error e)
-
{
-
AfxMessageBox("数据库关闭错误",MB_ICONEXCLAMATION);
-
exit(0);
-
}
5.总结
数据库操作多种多样,可以查查相关资料
这里只介绍了简单的一种操作,面向对象封装一下,直接调用即可

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.

Apache server is a powerful web server software that acts as a bridge between browsers and website servers. 1. It handles HTTP requests and returns web page content based on requests; 2. Modular design allows extended functions, such as support for SSL encryption and dynamic web pages; 3. Configuration files (such as virtual host configurations) need to be carefully set to avoid security vulnerabilities, and optimize performance parameters, such as thread count and timeout time, in order to build high-performance and secure web applications.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

This article will explain how to improve website performance by analyzing Apache logs under the Debian system. 1. Log Analysis Basics Apache log records the detailed information of all HTTP requests, including IP address, timestamp, request URL, HTTP method and response code. In Debian systems, these logs are usually located in the /var/log/apache2/access.log and /var/log/apache2/error.log directories. Understanding the log structure is the first step in effective analysis. 2. Log analysis tool You can use a variety of tools to analyze Apache logs: Command line tools: grep, awk, sed and other command line tools.

Nginx performance monitoring and troubleshooting are mainly carried out through the following steps: 1. Use nginx-V to view version information, and enable the stub_status module to monitor the number of active connections, requests and cache hit rate; 2. Use top command to monitor system resource occupation, iostat and vmstat monitor disk I/O and memory usage respectively; 3. Use tcpdump to capture packets to analyze network traffic and troubleshoot network connection problems; 4. Properly configure the number of worker processes to avoid insufficient concurrent processing capabilities or excessive process context switching overhead; 5. Correctly configure Nginx cache to avoid improper cache size settings; 6. By analyzing Nginx logs, such as using awk and grep commands or ELK

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

The Nginx current limit problem can be solved by: use ngx_http_limit_req_module to limit the number of requests; use ngx_http_limit_conn_module to limit the number of connections; use third-party modules (ngx_http_limit_connections_module, ngx_http_limit_rate_module, ngx_http_access_module) to implement more current limit policies; use cloud services (Cloudflare, Google Cloud Rate Limiting, AWS WAF) to DD
