MFC连接SQL时,出现未指定错误,怎么解决?主要程序代码如下
BOOL CConnectionDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// 初始化OLE/COM库环境
::CoInitialize(NULL);
try
{
// 创建Connection对象
m_pConnection.CreateInstance("ADODB.Connection");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect = "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=HrSys;Data Source=127.0.0.1;";
m_pConnection->Open(strConnect,"","",adModeUnknown);
// 判断连接状态是否为连接
if(m_pConnection->State == adStateOpen)
MessageBox("连接数据库");
// 关闭连接
m_pConnection->Close();
// 判断连接状态是否为关闭
if(m_pConnection->State == adStateClosed)
MessageBox("断开连接");
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(e.ErrorMessage());
}
return TRUE; // return TRUE unless you set the focus to a control
}

{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// 初始化OLE/COM库环境
::CoInitialize(NULL);
try
{
// 创建Connection对象
m_pConnection.CreateInstance("ADODB.Connection");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect = "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=HrSys;Data Source=127.0.0.1;";
m_pConnection->Open(strConnect,"","",adModeUnknown);
// 判断连接状态是否为连接
if(m_pConnection->State == adStateOpen)
MessageBox("连接数据库");
// 关闭连接
m_pConnection->Close();
// 判断连接状态是否为关闭
if(m_pConnection->State == adStateClosed)
MessageBox("断开连接");
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
AfxMessageBox(e.ErrorMessage());
}
return TRUE; // return TRUE unless you set the focus to a control
}

...全文
相关推荐
MFC连接SQL Server数据库
能过MFC连接SQL Server数据库,也可以远程进行连接,但是要加把server=改成ip,端口
vs2019 MFC ADO连接SQL server2012数据库 解决各种“坑”
vs2019 MFC ADO连接SQL server2012数据库 解决各种“坑”  转自 逗逗飞 略作修改 7533次阅读 2014-05-26 关注 通常情况下,一个给予ADO的数据库应用使用如下的过程操作数据源里的数据 (1) 创建一个Connection 对象。定义用于连接的字符串信息,包括数据源名称、用户ID、口令、连接超时 、 缺省数据库以及光标的位置。一个 Connection...
VS2013基于MFC连接sql server数据库以及增删改查
该文件是在VS2013下基于MFC连接数据库并完美实现增删改查,下载后只需要修改链接字就可以运行。谢谢采纳!
MFC ADO连接SQL Server
VS MFC ADO连接SQLServer实现增删改查操作。纯手打扣代码,有问题请留言。本篇文章博客地址 https://blog.csdn.net/qq_37529913
MFC连接SQL SERVER2008
MFC利用ODBC实现读写SQL SERVER数据库。


