我的问题感觉十分傻 却就是解决不了
在用VC连接SQL时 我写IMPORT了msado15.dll 然后初始化OLE/COM库环境 这两步都和网上的资料对照过 应该没问题 然后我写了语句
_ConnectionPtr m_pConnection;
if (FAILED(m_pConnection.CreateInstance("ADODB.Connection")))
{
AfxMessageBox("Create Instance failed!");
return;
}
以及之后的代码
现在问题时我运行程序时它就会出现Create Instance failed!"的对话框 也就是说上述的这个语句就没有创建对象实例成功 请问大家这是为什么啊 难道是哪里的设置问题吗 因为这直接影响下面的工作 所以很着急 希望大家帮帮我啊
在用VC连接SQL时 我写IMPORT了msado15.dll 然后初始化OLE/COM库环境 这两步都和网上的资料对照过 应该没问题 然后我写了语句
_ConnectionPtr m_pConnection;
if (FAILED(m_pConnection.CreateInstance("ADODB.Connection")))
{
AfxMessageBox("Create Instance failed!");
return;
}
以及之后的代码
现在问题时我运行程序时它就会出现Create Instance failed!"的对话框 也就是说上述的这个语句就没有创建对象实例成功 请问大家这是为什么啊 难道是哪里的设置问题吗 因为这直接影响下面的工作 所以很着急 希望大家帮帮我啊
解决方案 »
- VC程序,点击按钮后弹出Dialog,完善一下.
- 改变题标大小和属性页重叠,那位木子纵横大侠还在否?
- 在多文档程序中实现打开子窗口的功能遇到的问题
- 在dll里面的对话框上有windos Media Player控件,对话框就弹不出来,怎么回事?
- 关于调试的小问题,不过也急死我了!,在线等!
- 关于线程的问题,大家过来看看啊,分不够的话,可以再加.
- 如何统计一个特定文件夹下的文件数
- 如何操作线程呢?给100分!
- ///////////////////请大家帮我出个主意:要是有一天我做了一套可以全国推广的软件,我该怎样加密呢?
- 正想开发一个支持Plug-ins功能的程序,那位能提供一点资料信息?
- 请问哪有Visual Studio 2005正式版本下载?
- 为什么我写的控件当IE拖动滚动条的时候,刷新有问题?
{
ADODB::_ConnectionPtr pCn("ADODB.Connection");
ADODB::_RecordsetPtr pRs("ADODB.Recordset"); CString strSRC;
strSRC="Provider=SQLOLEDB.1;Password=doudou;Persist Security Info=True;User ID=sa;Initial Catalog=kj;Data Source=127.0.0.1";
CString strSQL = "select * from kjxx";
_variant_t varSRC(strSRC);
_variant_t varSQL(strSQL);
_bstr_t bstrSRC(strSRC); if(FAILED(pRs->Open(varSQL,varSRC,(ADODB::CursorTypeEnum)3,(ADODB::LockTypeEnum)3,(ADODB::CommandTypeEnum)1)))
{
AfxMessageBox("Open table failed!");
pRs.Release();
return 0;
} _variant_t vtwybsh;
_variant_t vtname;
COleDateTime t = COleDateTime::GetCurrentTime();
while (!pRs->adoEOF)
{
vtwybsh =pRs->Fields->GetItem("chr_wybsh")->GetValue();
vtname =pRs->Fields->GetItem("vchr_name")->GetValue();
printf( "chr_wybsh = '%s' vchr_name = '%s' time = '%s'\n", (char*) ((_bstr_t) vtwybsh),(char*) ((_bstr_t) vtname), t.Format("%Y-%m-%d"));
pRs->MoveNext();
} pRs.Release(); AfxMessageBox("ok!");
} catch (_com_error &e)
{
printf("Error:\n"); printf("Code = %08lx\n", e.Error()); printf("Code meaning = %s\n", (char*) e.ErrorMessage()); printf("Source = %s\n", (char*) e.Source()); printf("Description = %s\n", (char*) e.Description()); }
try
{
ADODB::_ConnectionPtr pCn("ADODB.Connection");
ADODB::_RecordsetPtr pRs("ADODB.Recordset"); CString strSRC;
strSRC="Provider=SQLOLEDB.1;Password=doudou;Persist Security Info=True;User ID=sa;Initial Catalog=kj;Data Source=127.0.0.1";
CString strSQL = "select * from kjxx";
_variant_t varSRC(strSRC);
_variant_t varSQL(strSQL);
_bstr_t bstrSRC(strSRC); if (FAILED(pCn->Open(bstrSRC,"","",-1)))
{
AfxMessageBox("Can not open Database!");
pCn.Release();
return 0;
}COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);pRs = pCn->Execute(_bstr_t(strSQL),&vtOptional,-1);pCn.Release();
/** if(FAILED(pRs->Open(varSQL,varSRC,(ADODB::CursorTypeEnum)3,(ADODB::LockTypeEnum)3,(ADODB::CommandTypeEnum)1)))
{
AfxMessageBox("Open table failed!");
pRs.Release();
return 0;
}
**/
_variant_t vtwybsh;
_variant_t vtname;
COleDateTime t = COleDateTime::GetCurrentTime();
while (!pRs->adoEOF)
{
vtwybsh =pRs->Fields->GetItem("chr_wybsh")->GetValue();
vtname =pRs->Fields->GetItem("vchr_name")->GetValue();
printf( "chr_wybsh = '%s' vchr_name = '%s' time = '%s'\n", (char*) ((_bstr_t) vtwybsh),(char*) ((_bstr_t) vtname), t.Format("%Y-%m-%d"));
pRs->MoveNext();
} pRs.Release(); AfxMessageBox("ok!");
} catch (_com_error &e)
{
printf("Error:\n"); printf("Code = %08lx\n", e.Error()); printf("Code meaning = %s\n", (char*) e.ErrorMessage()); printf("Source = %s\n", (char*) e.Source()); printf("Description = %s\n", (char*) e.Description()); } ::CoUninitialize();
m_conn->Open("Driver={SQL Server};Server=127.0.0.1;Database=down;User ID=sa;Password=sa","","",adModeUnknown);
m_set.CreateInstance(__uuidof(Recordset));
m_set->Open("SELECT * FROM bbs",
//_variant_t((IDispatch *)m_conn,true),
m_conn.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
RefreshData();
}catch(_com_error e){///捕捉异常
AfxMessageBox("连接数据库失败!");///显示错误信息
}偶也是刚学,上面的代码调试成功了,我是照别人的东东做了好久, 几乎和别人一模一样,也老是出错,不停地找代码, 不停的看文章, 终于搞定了我没用::CoInitialize(NULL);这个,照样能运行