我是有两个程序同时连一个mdb文件,连接的字符串是
pDBConnect->Open((_bstr_t)m_strConnection,"","",-1);
不知道改如何设置才可以让他们同时都可以跑啊。
6 个解决方案
#1
刚才我这么测试了一下:
//打开数据库
CREATEINSTANCE(m_piConnection,Connection);
m_piConnection->Open(_bstr_t(sConnStr),_bstr_t(L""),_bstr_t(L""),adModeUnknown);
theApp.m_ErrLog.WriteErrorInfo("CDataBase::Connect","成功连接数据库");
_ConnectionPtr testPtr;
CREATEINSTANCE(testPtr,Connection);
testPtr->Open(_bstr_t(sConnStr),_bstr_t(L""),_bstr_t(L""),adModeUnknown);
没有问题。两个链接都成功了。
//打开数据库
CREATEINSTANCE(m_piConnection,Connection);
m_piConnection->Open(_bstr_t(sConnStr),_bstr_t(L""),_bstr_t(L""),adModeUnknown);
theApp.m_ErrLog.WriteErrorInfo("CDataBase::Connect","成功连接数据库");
_ConnectionPtr testPtr;
CREATEINSTANCE(testPtr,Connection);
testPtr->Open(_bstr_t(sConnStr),_bstr_t(L""),_bstr_t(L""),adModeUnknown);
没有问题。两个链接都成功了。
#2
Open的时候不要设置独享选项
用adModeUnknown
用adModeUnknown
#3
1.stdafx.h中加入
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
2。app文件的InitInstance中加入
if(S_OK!=OleInitialize(NULL))
{
AfxMessageBox("初始化COM组件库错误");
}
下面就是数据库操作
_ConnectionPtr pConn;
_RecordsetPtr pRs;
CString strSQL;
pConn.CreateInstance(__uuidof(Connection));
pConn->CursorLocation=adUseClient;
strSQL="Provider=SQLOLEDB;Server=Gooyan;DataBase=SkillsBox;UID=sa;PWD=aaaaaaaa";
pConn->Open(_bstr_t(strSQL),"","",-1);
pRs.CreateInstance(__uuidof(Recordset));
pRs->CursorLocation=adUseClient;
pRs->PutActiveConnection(pConn.GetInterfacePtr());
pRs1.CreateInstance(__uuidof(Recordset));
pRs1->CursorLocation=adUseClient;
pRs1->PutActiveConnection(pConn.GetInterfacePtr());
strSQL="select * from catalog order by cataid";
pRs->Open(_bstr_t(strSQL),vtMissing,adOpenDynamic,adLockBatchOptimistic,adCmdText);
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
2。app文件的InitInstance中加入
if(S_OK!=OleInitialize(NULL))
{
AfxMessageBox("初始化COM组件库错误");
}
下面就是数据库操作
_ConnectionPtr pConn;
_RecordsetPtr pRs;
CString strSQL;
pConn.CreateInstance(__uuidof(Connection));
pConn->CursorLocation=adUseClient;
strSQL="Provider=SQLOLEDB;Server=Gooyan;DataBase=SkillsBox;UID=sa;PWD=aaaaaaaa";
pConn->Open(_bstr_t(strSQL),"","",-1);
pRs.CreateInstance(__uuidof(Recordset));
pRs->CursorLocation=adUseClient;
pRs->PutActiveConnection(pConn.GetInterfacePtr());
pRs1.CreateInstance(__uuidof(Recordset));
pRs1->CursorLocation=adUseClient;
pRs1->PutActiveConnection(pConn.GetInterfacePtr());
strSQL="select * from catalog order by cataid";
pRs->Open(_bstr_t(strSQL),vtMissing,adOpenDynamic,adLockBatchOptimistic,adCmdText);
#4
ACCESS 2000
_ConnectionPtr m_pConn;
CString m_sConn="Provider=Microsoft.Jet.OLEDB.4.0.1;Data Source=d:\\db1.mdb";
m_pConn.CreateInstance("ADODB.Connection");
try
{
HRESULT hr=m_pConn->Open((_bstr_t)m_sConn,"","",adConnectUnspecified);
if (FAILED(hr))
{
AfxMessageBox("不能连接数据库 source!");
return FALSE;
}
}
catch(_com_error e)
{
AfxMessageBox("不能连接数据库 error!");
return FALSE;
}
_ConnectionPtr m_pConn;
CString m_sConn="Provider=Microsoft.Jet.OLEDB.4.0.1;Data Source=d:\\db1.mdb";
m_pConn.CreateInstance("ADODB.Connection");
try
{
HRESULT hr=m_pConn->Open((_bstr_t)m_sConn,"","",adConnectUnspecified);
if (FAILED(hr))
{
AfxMessageBox("不能连接数据库 source!");
return FALSE;
}
}
catch(_com_error e)
{
AfxMessageBox("不能连接数据库 error!");
return FALSE;
}
#5
同意,不要用独占方式打开数据库
#6
happyparrot(快乐鹦鹉)老兄说的不错,但OPEN()的最后一个参数为什么要是adModeUnknown?借问一下:adModeUnknown=0,与-1有什么不同?用-1的时候就不太好使:)
#1
刚才我这么测试了一下:
//打开数据库
CREATEINSTANCE(m_piConnection,Connection);
m_piConnection->Open(_bstr_t(sConnStr),_bstr_t(L""),_bstr_t(L""),adModeUnknown);
theApp.m_ErrLog.WriteErrorInfo("CDataBase::Connect","成功连接数据库");
_ConnectionPtr testPtr;
CREATEINSTANCE(testPtr,Connection);
testPtr->Open(_bstr_t(sConnStr),_bstr_t(L""),_bstr_t(L""),adModeUnknown);
没有问题。两个链接都成功了。
//打开数据库
CREATEINSTANCE(m_piConnection,Connection);
m_piConnection->Open(_bstr_t(sConnStr),_bstr_t(L""),_bstr_t(L""),adModeUnknown);
theApp.m_ErrLog.WriteErrorInfo("CDataBase::Connect","成功连接数据库");
_ConnectionPtr testPtr;
CREATEINSTANCE(testPtr,Connection);
testPtr->Open(_bstr_t(sConnStr),_bstr_t(L""),_bstr_t(L""),adModeUnknown);
没有问题。两个链接都成功了。
#2
Open的时候不要设置独享选项
用adModeUnknown
用adModeUnknown
#3
1.stdafx.h中加入
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
2。app文件的InitInstance中加入
if(S_OK!=OleInitialize(NULL))
{
AfxMessageBox("初始化COM组件库错误");
}
下面就是数据库操作
_ConnectionPtr pConn;
_RecordsetPtr pRs;
CString strSQL;
pConn.CreateInstance(__uuidof(Connection));
pConn->CursorLocation=adUseClient;
strSQL="Provider=SQLOLEDB;Server=Gooyan;DataBase=SkillsBox;UID=sa;PWD=aaaaaaaa";
pConn->Open(_bstr_t(strSQL),"","",-1);
pRs.CreateInstance(__uuidof(Recordset));
pRs->CursorLocation=adUseClient;
pRs->PutActiveConnection(pConn.GetInterfacePtr());
pRs1.CreateInstance(__uuidof(Recordset));
pRs1->CursorLocation=adUseClient;
pRs1->PutActiveConnection(pConn.GetInterfacePtr());
strSQL="select * from catalog order by cataid";
pRs->Open(_bstr_t(strSQL),vtMissing,adOpenDynamic,adLockBatchOptimistic,adCmdText);
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
2。app文件的InitInstance中加入
if(S_OK!=OleInitialize(NULL))
{
AfxMessageBox("初始化COM组件库错误");
}
下面就是数据库操作
_ConnectionPtr pConn;
_RecordsetPtr pRs;
CString strSQL;
pConn.CreateInstance(__uuidof(Connection));
pConn->CursorLocation=adUseClient;
strSQL="Provider=SQLOLEDB;Server=Gooyan;DataBase=SkillsBox;UID=sa;PWD=aaaaaaaa";
pConn->Open(_bstr_t(strSQL),"","",-1);
pRs.CreateInstance(__uuidof(Recordset));
pRs->CursorLocation=adUseClient;
pRs->PutActiveConnection(pConn.GetInterfacePtr());
pRs1.CreateInstance(__uuidof(Recordset));
pRs1->CursorLocation=adUseClient;
pRs1->PutActiveConnection(pConn.GetInterfacePtr());
strSQL="select * from catalog order by cataid";
pRs->Open(_bstr_t(strSQL),vtMissing,adOpenDynamic,adLockBatchOptimistic,adCmdText);
#4
ACCESS 2000
_ConnectionPtr m_pConn;
CString m_sConn="Provider=Microsoft.Jet.OLEDB.4.0.1;Data Source=d:\\db1.mdb";
m_pConn.CreateInstance("ADODB.Connection");
try
{
HRESULT hr=m_pConn->Open((_bstr_t)m_sConn,"","",adConnectUnspecified);
if (FAILED(hr))
{
AfxMessageBox("不能连接数据库 source!");
return FALSE;
}
}
catch(_com_error e)
{
AfxMessageBox("不能连接数据库 error!");
return FALSE;
}
_ConnectionPtr m_pConn;
CString m_sConn="Provider=Microsoft.Jet.OLEDB.4.0.1;Data Source=d:\\db1.mdb";
m_pConn.CreateInstance("ADODB.Connection");
try
{
HRESULT hr=m_pConn->Open((_bstr_t)m_sConn,"","",adConnectUnspecified);
if (FAILED(hr))
{
AfxMessageBox("不能连接数据库 source!");
return FALSE;
}
}
catch(_com_error e)
{
AfxMessageBox("不能连接数据库 error!");
return FALSE;
}
#5
同意,不要用独占方式打开数据库
#6
happyparrot(快乐鹦鹉)老兄说的不错,但OPEN()的最后一个参数为什么要是adModeUnknown?借问一下:adModeUnknown=0,与-1有什么不同?用-1的时候就不太好使:)