VC中如何向SQL中插入数据,在线等!!!

xtarwgd 2009-01-12 11:51:09
VC中如何向SQL中插入数据

本人是一VC新手,不知道在VC中如何去连接SQL数据库,也不知道该怎么写代码,谢谢各位!!
...全文
1043 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeflay888 2009-10-01
  • 打赏
  • 举报
回复
我的是用ODBC写的一个删除操作 红色部分不知道怎么去写,谁能帮我看下,谢谢各位!
char m_szID[30+1] ;
m_hListSpxx.GetItemText(m_hListSpxx.GetSelectionMark(), 0, m_szID, sizeof(m_szID)) ;
CDatabase m_db;
m_db.OpenEx(_T("DSN=SellManage;"),CDatabase::noOdbcDialog);
CString strSQL;
strSQL="delete from tb_merchandiseinfo where ID = 'm_szID'";
m_db.ExecuteSQL(strSQL);
hunt2088 2009-10-01
  • 打赏
  • 举报
回复
1楼讲的很详细了 不懂的话再问
myjianghe 2009-02-25
  • 打赏
  • 举报
回复
代码如前面所述,步骤是
1.初始化COM库,
2.连接数据库,
3.打开记录表
BlackN 2009-02-23
  • 打赏
  • 举报
回复
好文章就是要顶
ten_fly 2009-02-13
  • 打赏
  • 举报
回复
mark ,学习一下
zteclx 2009-01-30
  • 打赏
  • 举报
回复
很详细了。
闪破风浪 2009-01-17
  • 打赏
  • 举报
回复
看看《ADO开发指南》书~
youhao1999 2009-01-17
  • 打赏
  • 举报
回复
ADO方法
连接:
HRESULT hr;
try
{
//实例化连接对象
hr=m_pConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
//设置连接串属性为UDL文件
m_pConnection->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.mdb";
//设置等待连接打开的时间为20秒
m_pConnection->ConnectionTimeout=20;
hr=m_pConnection->Open("","","",adConnectUnspecified);
if(FAILED(hr))
{
AfxMessageBox("open fail!");
return TRUE;
}
}
else
{
AfxMessageBox("createinstance of Connection fail!");
return TRUE;
}
}
catch (_com_error e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
AfxMessageBox(bstrSource+bstrDescription);
return TRUE;
}
return TRUE;
}

插入数据:
_RecordsetPtr pRentRecordset;
pRentRecordset.CreateInstance(__uuidof(Recordset));
HRESULT hr;
try
{
hr = pRentRecordset->Open("select * from ",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);

if(SUCCEEDED(hr))
{
pRentRecordset->AddNew();
pRentRecordset->PutCollect("id",_variant_t(m_code));
pRentRecordset->PutCollect("name",_variant_t(m_name));
pRentRecordset->PutCollect("page",_variant_t(m_page));
pRentRecordset->PutCollect("finishtime",_variant_t("2008-08-08"));
pRentRecordset->PutCollect("moditime",_variant_t("2008-08-08"));
//pRentRecordset->PutCollect("tips",_variant_t("0"));
pRentRecordset->Update();
}
}
catch(_com_error e)
{
MessageBox(e.Description());
return;
}
pRentRecordset->Close();
pRentRecordset=NULL;
MessageBox("记录添加成功~","提示",MB_OK|MB_ICONINFORMATION);
}
zaodt 2009-01-13
  • 打赏
  • 举报
回复
首先,要选择读写 SQL SERVER 数据库的方法,常用两种:

1、VC + ODBC + SQL SERVER

2、VC + ADO + SQL SERVER(推荐)


下面这篇文章是读写 ACCESS MDB 数据库的文章,但读写 SQL SERVER 也差不多:


【直接通过ADO操作Access数据库】

http://www.vckbase.com/document/viewdoc/?id=496
littlefangMFC 2009-01-12
  • 打赏
  • 举报
回复
1.在工程的Cxx.cpp(在A工程中的CAApp类中)的InitInstance()函数中
/*********初始化COM库***************/
在MFC中可以用AfxOleInit();
非MFC环境中用:
CoInitialize(NULL);
CoUnInitialize();
2.在StdAfx.h中
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
3.为程序添加数据库连接函数OnInitADOConn(),函数名可以任取
void CDataConDlg::OnInitADOConn()
{
/***********连接数据库**************/
try
{
m_pConnection.CreateInstance("ADODB.Connection");
/*****************Catalog为你的数据库的名称**********************************/
CString strConnect="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=FXL;Data Source=.;";
m_pConnection->Open((_bstr_t)strConnect,"","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
/************数据库连接成功********************/

}
4.需要操作数据库时:
a.添加成员变量
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
b.建立连接,创建记录集
OnInitADOConn();
//UpdateData(true);
_bstr_t sql;
sql="select * from FXL_YG";//FXL_YG为数据库中的表名
m_pRecordset.CreateInstance(_uuidof(Recordset));
m_pRecordset->Open(sql,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
c.进行你需要的操作(本例是为数据库添加一条新的记录)
try
{
m_pRecordset->AddNew();
m_pRecordset->PutCollect("编号",(_bstr_t)number);
m_pRecordset->PutCollect("姓名",(_bstr_t)name);
m_pRecordset->PutCollect("学历",(_bstr_t)culture);
m_pRecordset->Update();
}
catch(_com_error e)
{
AfxMessageBox("编号重复");
return;
}
d.关闭记录集和连接
m_pRecordset->Close();
m_pConnection->Close();
CoUninitialize();//关闭com
chenghu19860527 2009-01-12
  • 打赏
  • 举报
回复
UP
analysefirst 2009-01-12
  • 打赏
  • 举报
回复
很详细了,就不写了。要不看本人的博客上也有。
R_hgt 2009-01-12
  • 打赏
  • 举报
回复
mark
yekoufeng 2009-01-12
  • 打赏
  • 举报
回复
楼上很详细了
也可以这样.
str7.Format("INSERT INTO %s(P_NAME,P_CTYPE,P_DESC) values('%s','%d','%s')", m_tablename, m_pointname, m_ctype, m_description);
TRACE(str7);
_variant_t sql;
sql = str7;
long hr;

try
{
hr = m_pConnection->Execute((_bstr_t)sql, NULL, adCmdText);
if(!SUCCEEDED(hr))
{
return ;
}
}
catch(_com_error &e)
{
AfxMessageBox(e.Description());
// return;
exit(1);//不正常退出
}

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧