前面 介绍:mfc 链接 access 2007 数据库
接下来就来说说怎么插入数据到access 2007 数据库(网上找了下,说的有些模糊,直接来看看效果)
主要步骤:
1 先用sprintf生成SQL语句
2 再执行SQL语句就可以了
效果如下:
主要代码:
CString strConn,strDBName;
//serv103_serv_link_acces();
if(m_pConnection->State)
{
m_pRecordset.CreateInstance("ADODB.Recordset");
//m_pConnection->Close(); ///如果已经打开了连接则关闭它
try
{
int m_id =1;
int m_qh =1;
int m_value =1;
//char *SQL = "";
char buffer[256];
sprintf(buffer,"insert into dzvalues(dzId,dzQh,dzValue) values('%d','%d','%d')",m_id,m_qh,m_value);
m_pConnection->Execute(buffer,&RecordsAffected,adCmdText);
//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);
}
catch (_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
//m_pConnection->Close(); ///如果已经打开了连接则关闭它