Home > Database > Mysql Tutorial > MFC使用ADO操作SQLSERVER数据库

MFC使用ADO操作SQLSERVER数据库

WBOY
Release: 2016-06-07 15:46:09
Original
1668 people have browsed it

欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入 首先,要引入msado15.dll库文件,该文件是ADO操作的库: #import "C:\ProgramFiles\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF", "adoBOF") 然后

欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入

  首先,要引入msado15.dll库文件,该文件是ADO操作的库:

  #import "C:\ProgramFiles\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF", "adoBOF")

  然后在程序的初始化处,加入如下语句,初始化ADO的COM组件:

  if (!AfxOleInit())                          //初?始o?化?$OLE/COM库a

  {

  AfxMessageBox(_T("OLE init Error"));

  returnFALSE;

  }

  最后定义一个执行SQL语句的函数:

  /*******************************************/

  */

  /*******************************************/

  void SQLFunction(CStringstr)

  {

  _ConnectionPtrpConn;

  const_bstr_t strSRC= "Provider=SQLOLEDB;Server=(local);DataBase=yourDataBase;uid=sa;pwd=yourpassword;";

  if(FAILED(pConn.CreateInstance("ADODB.Connection")))

  {

  AfxMessageBox(_T("Create Instance failed!"));

  exit(0);

  }

  try{

  pConn->Open(strSRC,"","",-1);

  //执行SQL语句

  _variant_t  RecordsAffected;

  pConn->Execute((_bstr_t)str,&RecordsAffected,adCmdText);

  //关闭数据库

  pConn->Close();

  pConn.Release();

  }

  catch (_com_error &e)

  {

  AfxMessageBox(e.Description());

  exit(0);

  }

  }

  每次需要执行SQL操作时,只需要向SQLFunction函数传入SQL语句即可操作进行。

  数据库连接字符中

  "Provider=SQLOLEDB;Server=(local);DataBase=yourDataBase;uid=sa;pwd=yourpassword;";

  Server可以直接用IP地址。

MFC使用ADO操作SQLSERVER数据库

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template