菜鸟一个,大侠教我一个如何用VC++连接SQL数据库的实例并进行几个增加记录和修改记录的实例么!
用一个已经创立好的MFC空程序,进行一个SQL数据库连接!越简单越好,我是想通过简单的例子自己不断的去扩充!可能问题描述不清!希望大侠莫见怪哈.....可在线HI我~!Q...
用一个已经创立好的MFC空程序,进行一个SQL数据库连接!越简单越好,我是想通过简单的例子自己不断的去扩充!可能问题描述不清!希望大侠莫见怪哈.....
可在线HI我~!
QQ:1540982547 展开
可在线HI我~!
QQ:1540982547 展开
展开全部
可以用ADO进行连接
首先初始化OLE/COM库环境
BOOL CTestApp::InitInstance()
{ //初始化OLE/COM库环境
AfxOleInit();
}
再在stdafx.h中引入ADO库文件
#include 〈comdef.h〉
#import "c:\program files\common files\system\ado\msado15.dll" \
no_namespace \
rename ("EOF","adoEOF")
有三种指针
_ConnectionPtr m_pConnection;//创建一个数据连接或执行一条不返回任何结果的SQL语句
_RecordsetPtr m_pRecordset; //是一个记录集对象
_CommandPtr m_pCommand//返回一个记录集
初始化指针
m_pConnection.CreateInstance(_uuidof(Connection));
m_pRecordset.CreateInstance(_uuidof(Recordset));
首先连接数据库
m_pConnection-Open("/*连接字符串*/","","",0)
然后就可以使用sql语句了
m_pRecordset->Open("/*sql语句*/",(IDispatch*)m_pConnection,
adOpenDynamic,
adLockOptimistic,
adCmdText
m_pConnection->Execute(“/*sql语句*/”,NULL,adCmdText);
)
首先初始化OLE/COM库环境
BOOL CTestApp::InitInstance()
{ //初始化OLE/COM库环境
AfxOleInit();
}
再在stdafx.h中引入ADO库文件
#include 〈comdef.h〉
#import "c:\program files\common files\system\ado\msado15.dll" \
no_namespace \
rename ("EOF","adoEOF")
有三种指针
_ConnectionPtr m_pConnection;//创建一个数据连接或执行一条不返回任何结果的SQL语句
_RecordsetPtr m_pRecordset; //是一个记录集对象
_CommandPtr m_pCommand//返回一个记录集
初始化指针
m_pConnection.CreateInstance(_uuidof(Connection));
m_pRecordset.CreateInstance(_uuidof(Recordset));
首先连接数据库
m_pConnection-Open("/*连接字符串*/","","",0)
然后就可以使用sql语句了
m_pRecordset->Open("/*sql语句*/",(IDispatch*)m_pConnection,
adOpenDynamic,
adLockOptimistic,
adCmdText
m_pConnection->Execute(“/*sql语句*/”,NULL,adCmdText);
)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
_ConnectionPtr m_pConnection;
CoInitialize(NULL);//com编程用的,用于初始化com库
m_pConnection.CreateInstance(__uuidof(Connection));
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydata.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
MessageBox(hwnd,"数据库连接失败,确认数据库Demo.mdb是否在当前路径下!","ggg",0);
return FALSE;
}
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset->Open("SELECT * FROM user_Info", m_pConnection.GetInterfacePtr(), adOpenDynamic,
LockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
MessageBox(hwnd,e->ErrorMessage(),"err",0);
}
_variant_t var;
string strName,strAge;
try
{
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else
{
MessageBox(hwnd,"表内数据为空","err",0);
return 0;
}
// 读入库中各字段并加入列表框中
while(!m_pRecordset->adoEOF)
{
SetWindowText(edit, " ");///
Sleep(1300);//单位毫秒
char temp[80];
var = m_pRecordset->GetCollect("user_ID");
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
strcpy( temp, strName.c_str() );
var = m_pRecordset->GetCollect("user_PWD");
if(var.vt != VT_NULL)
strAge = (LPCSTR)_bstr_t(var);
strcat(temp,strAge.c_str());
SetWindowText(edit, temp);///
/// m_AccessList.AddString( strName + " --> "+strAge );
m_pRecordset->MoveNext();
//#pragma code ("kernel32.dll")
//VOID WINAPI Sleep ( DWORD dwMilliseconds );
//#pragma code()
// 默认列表指向第一项,同时移动记录指针并显示
//m_AccessList.SetCurSel(0);
}
}
catch(_com_error *e)
{
MessageBox(hwnd,e->ErrorMessage(),"err",1);
}
CoInitialize(NULL);//com编程用的,用于初始化com库
m_pConnection.CreateInstance(__uuidof(Connection));
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydata.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
MessageBox(hwnd,"数据库连接失败,确认数据库Demo.mdb是否在当前路径下!","ggg",0);
return FALSE;
}
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset->Open("SELECT * FROM user_Info", m_pConnection.GetInterfacePtr(), adOpenDynamic,
LockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
MessageBox(hwnd,e->ErrorMessage(),"err",0);
}
_variant_t var;
string strName,strAge;
try
{
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else
{
MessageBox(hwnd,"表内数据为空","err",0);
return 0;
}
// 读入库中各字段并加入列表框中
while(!m_pRecordset->adoEOF)
{
SetWindowText(edit, " ");///
Sleep(1300);//单位毫秒
char temp[80];
var = m_pRecordset->GetCollect("user_ID");
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
strcpy( temp, strName.c_str() );
var = m_pRecordset->GetCollect("user_PWD");
if(var.vt != VT_NULL)
strAge = (LPCSTR)_bstr_t(var);
strcat(temp,strAge.c_str());
SetWindowText(edit, temp);///
/// m_AccessList.AddString( strName + " --> "+strAge );
m_pRecordset->MoveNext();
//#pragma code ("kernel32.dll")
//VOID WINAPI Sleep ( DWORD dwMilliseconds );
//#pragma code()
// 默认列表指向第一项,同时移动记录指针并显示
//m_AccessList.SetCurSel(0);
}
}
catch(_com_error *e)
{
MessageBox(hwnd,e->ErrorMessage(),"err",1);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-08-12
展开全部
http://icese.net/icetool/other/testdatabasedll.rar
可参考
//连接Access数据库
retCode=icePub_databaseConnect(DBTYPE_Access, "TEST", "", szFile, "", "", "", &m_henv, &m_hdbc);
//连接SQL Server数据库
//icePub_databaseConnect(DBTYPE_SQLServer, "sqltest", "test", "", "(local)", "sa", "", &m_henv, &m_hdbc);
//连接MySQL数据库
//icePub_databaseConnect(DBTYPE_MySQL, "TEST", "test", "", "localhost", "root", "123456", &m_henv, &m_hdbc);
//连接DB2数据库
//icePub_databaseConnect(DBTYPE_DB2, "TEST", "TEST", "", "DB2", "db2admin", "123456", &m_henv, &m_hdbc);
//连接Oracle数据库
//icePub_databaseConnect(DBTYPE_Oracle, "TEST1", "", "", "bjrocket", "system", "system", &m_henv, &m_hdbc);
可参考
//连接Access数据库
retCode=icePub_databaseConnect(DBTYPE_Access, "TEST", "", szFile, "", "", "", &m_henv, &m_hdbc);
//连接SQL Server数据库
//icePub_databaseConnect(DBTYPE_SQLServer, "sqltest", "test", "", "(local)", "sa", "", &m_henv, &m_hdbc);
//连接MySQL数据库
//icePub_databaseConnect(DBTYPE_MySQL, "TEST", "test", "", "localhost", "root", "123456", &m_henv, &m_hdbc);
//连接DB2数据库
//icePub_databaseConnect(DBTYPE_DB2, "TEST", "TEST", "", "DB2", "db2admin", "123456", &m_henv, &m_hdbc);
//连接Oracle数据库
//icePub_databaseConnect(DBTYPE_Oracle, "TEST1", "", "", "bjrocket", "system", "system", &m_henv, &m_hdbc);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询