MFC中SQL Sever数据库的增删查改

1.首先得要ADO中connection对象链接数据库

2.然后介绍记录集对象:_RecordsetPt 类型   :这相当于数据库中的一个表的对象

3.奉出我当时具体的代码,这是添加在按钮事件通知函数中,详细解释在代码的后边

增加:

        //创建记录集
        _RecordsetPtr m_pRecordset;
	m_pRecordset.CreateInstance(__uuidof(Recordset));

	//得到用户从编辑框、下拉框输入的值
	CString strsql, stu_Id, stu_Name, stu_Sex, stu_Institute, stu_Class, stu_Home, stu_Phone, stu_Birthday, stu_Major;
	int i = Sex.GetCurSel();
	Sex.GetLBText(i,stu_Sex);
	int j = Institute.GetCurSel();
	Institute.GetLBText(i,stu_Institute);
	Id.GetWindowTextW(stu_Id);
	Name.GetWindowTextW(stu_Name);
	Class.GetWindowTextW(stu_Class);
	Home.GetWindowTextW(stu_Home);
	Phone.GetWindowTextW(stu_Phone);
	Major.GetWindowTextW(stu_Major);
	Birthday.GetWindowTextW(stu_Birthday);


	//添加数据库语言,括号中的字符串就是操作数据库进行添加操作的语言,其中
         //StuInfo是我数据库中的一个学生信息的表
	strsql.Format(TEXT("insert into StuInfo( Id, Name, Sex, Institue, Major, Class, Birthday, Home, Phone) values( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')"),
		stu_Id, stu_Name, stu_Sex, stu_Institute, stu_Major, stu_Class, stu_Birthday, stu_Home, stu_Phone);

	//连接到数据库并进行添加操作,将strsql添加在第一个参数,m_pConnection就是当所连接的数 据库的connectoin对象
	try
	{ 
		m_pRecordset->Open((_variant_t)strsql, m_pConnection.GetInterfacePtr(), adOpenKeyset, adLockOptimistic, adCmdText);
		AfxMessageBox(TEXT("添加成功"));
	}
	catch (...)
	{
		AfxMessageBox(_T("添加失败")); 
	}

        m_pRecordset->Close();//如果有中断报错,删掉close函数 
	m_pRecordset = NULL;

删除:

         //创建记录集对象
	_RecordsetPtr m_pRecordset;
	m_pRecordset.CreateInstance(__uuidof(Recordset));

	//保存编辑框中数据

	CString strsql, stu_Id;

	Search_Id.GetWindowTextW(stu_Id);

        //数据库语言,选择

	strsql = TEXT("select * from StuInfo");

        //将查询数据导入m_pRecordset数据容器

	m_pRecordset = m_pConnection->Execute(_bstr_t(strsql), NULL, adCmdText);
	int Num = 0;			

	//将记录集获取的数据一一与输入的数据进行比较

	while (!m_pRecordset->adoEOF)//EOF判断是否到末尾

	{

		CString Info_Id = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem("Id")->Value;
                

                 //如果相等即查找到
		if (stu_Id.Compare(Info_Id) == 0)
		{
                         Num++;

                        //以下是获取数据库中每一行的内容
			CString Info_Name = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem("Name")->Value;
			CString Info_Sex = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem("Sex")->Value;
			CString Info_Institue = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem("Institue")->Value;
			CString Info_Major = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem("Major")->Value;
			CString Info_Class = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem("Class")->Value;
			CString Info_Birthday = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem("Birthday")->Value;
			CString Info_Home = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem("Home")->Value;
			CString Info_Phone = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem("Phone")->Value;
			break;
		}

                 //记录下移
		m_pRecordset->MoveNext();
	}
	if (!Num)
	{
		AfxMessageBox(_T("无此学生的任何记录!"));
	}
	m_pRecordset->Close();//如果有中断报错,删掉close函数 
	m_pRecordset = NULL;

修改:

         //创建记录集对象
	_RecordsetPtr m_pRecordset;
	m_pRecordset.CreateInstance(__uuidof(Recordset));


	//获得用户输入值
	CString strsql, stu_Id, stu_Name,Content;
	Modify_Sex.GetWindowTextW(stu_Name);
	Modify_Name.GetWindowTextW(stu_Id);
	int Select = InfoCategory.GetCurSel();
	Modify_Contents.GetWindowTextW(Content);
	
        //数据库的执行语言
        //update指更新  set Id:是指修改Id后的新值,  where Id and Name:是指想要修改学生的Id和姓名,也可以只写Id前提是Id必须唯一
	strsql.Format(_T("update StuInfo set Id='%s' where Id = '%s' and Name = '%s'"), Content,stu_Id,stu_Name );
		
	//删除
	try
	{ 
		m_pConnection->Execute(_bstr_t(strsql), 0, adCmdText);
		AfxMessageBox(TEXT("修改成功"));
	}
	catch (_com_error e)
	{
		MessageBox(e.Description());
		return;
	}
	m_pRecordset = NULL;

删除:

删除有两种方法:

(1):使用connection对象

	
        //获取用户值
        CString strSql,stu_Id,stu_Name;
	Delete_Id.GetWindowTextW(stu_Id);
	Delete_Name.GetWindowTextW(stu_Name);
	//添加数据库的语言
	strSql.Format(TEXT("delete from StuInfo where Id = %s "),stu_Id );
        //执行删除
	try
	{
		m_pConnection->Execute(_bstr_t(strSql), 0, adCmdText);
		MessageBox(TEXT("删除成功!"));
	}
        //捕捉错误原因
	catch (_com_error e)
	{
		MessageBox(e.Description());
		return ;
	}

(2):使用记录集对象

        CString user_Name;
	Revise.GetWindowTextW(user_Name);

        //定义一个 记录集对象,负责查找想要删除的学生信息所在当前表中的行号

	_RecordsetPtr m_pRecordset;
	m_pRecordset.CreateInstance(__uuidof(Recordset));

        //利用Execute函数将查询数据导入m_pRecordset数据容器

	m_pRecordset = Land_m_pConnection->Execute(_bstr_t(TEXT("select * from UserPassWord")), NULL, adCmdText);//	

	//定位输入用户的名称在哪一行
	int Num = 0;																		              
        //将记录集获取的数据一一与输入的数据进行比较
	while (!m_pRecordset->adoEOF)//EOF判断是否到末尾
	{
		CString Info_Name = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem("UserName")->Value;

		if (user_Name.Compare(Info_Name) == 0)
		{
			break;
		}
		m_pRecordset->MoveNext();
		Num++;
	}
	//重新定义一个记录集,利用Open打开
	_RecordsetPtr new_m_pRecordset;
	new_m_pRecordset.CreateInstance(__uuidof(Recordset));
	new_m_pRecordset->Open(TEXT("select * from UserPassWord"),_variant_t((IDispatch*)Land_m_pConnection, true),
		adOpenStatic,
		adLockOptimistic,
		adCmdText);
        //执行删除
	try
	{
		new_m_pRecordset->MoveFirst();
		new_m_pRecordset->Move(Num);                //移动到Num个记录
		new_m_pRecordset->Delete(adAffectCurrent);  //删除当前记录
		MessageBox(TEXT("已经成功删除该用户!"));
		new_m_pRecordset->Update();                  //对数据库进行更新
	}
	catch (_com_error e)
	{
		MessageBox(e.Description());
	}

我当时在写代码的时候,第一次写connection对象是正常的,没有异常,但后来一直在执行时候有中断的异常,错误没找来,想不通啊,只是通过使用了第二种方法才解决,后来我想,可能是当时第一次使用connection对象删除或者在初始化连接的时候没有调用close进行关闭连接造成的。可是程序的其他的cpp功能还需要连接数据库,为了避免不停的打开关闭,就直接使用记录集对象了。

发布了37 篇原创文章 · 获赞 12 · 访问量 9265

猜你喜欢

转载自blog.csdn.net/weixin_43265881/article/details/96152015