_ConnectionPtr pConn("ADODB.Connection");
//定义数据库连接字符串
_bstr_t Connection = "Provider=SQLOLEDB;Server=127.0.0.1;Database=LoginDemo;Uid=; PWD= ";
//打开数据库连接
pConn->Open(Connection, "", "", adModeUnknown);
我连接的时候把 LoginDome.mdf 这个数据库文件和日志文件LoginDemo_log.ldf放在工程下面。
然后代码中用上面的语句访问。 可总是失败。是什么原因啊??
连接字串中的 用户名UID 和密码PWD 怎么设置啊???
高水给个范例吧。。
15 个解决方案
#1
HRESULT hr;
CString strDBSource;
_ConnectionPtr m_pConn;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;
//SQL Server数据库的连接串
strDBSource="Provider=SQLOLEDB.1;Password=abcde;Persist Security Info=True;User ID=sa;Initial Catalog=wzWebLab;Data Source=.;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=LIUZG;Use Encryption for Data=False;Tag with column collat"
_bstr_t strCnn(strDBSource);
try{
hr=m_pConn.CreateInstance("ADODB.Connection");
if(FAILED(hr)){
return FALSE;
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
return FALSE;
}
m_pConn->ConnectionTimeout=15;
m_pConn->Mode=adModeReadWrite;
m_pConn->IsolationLevel=adXactIsolated;
m_pConn->Attributes&=!adXactCommitRetaining;
m_pConn->Attributes&=!adXactAbortRetaining;
try{
m_pConn->Open(strCnn,"","",NULL);
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
return FALSE;
}catch(...){
return FALSE;
}
try{
m_pCommand.CreateInstance("ADODB.Command" ); // creating command object
} catch(...){
return false;
}
try{
hr=m_pRecordset.CreateInstance("ADODB.Recordset");
}catch(...){
return false;
}
//Recordset例子
_variant_t vConn;
_variant_t sqlStr=_T("select * from table1");
vConn=(IDispatch*) m_pConn;
try{
hr=m_pRecordset->Open(sqlStr,vConn,adOpenStatic,adLockOptimistic,-1);
if(!FAILED(hr)){
while (!m_pRecordset->EndOfFile){
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
CString mValue=varTemp.bstrVal;
m_pRecordset->MoveNext();
}
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE("%s\n",(LPCSTR) bstrDescription);
}catch(...){
}
m_pRecordset->Close();
//Command例子
try{//
passing string value as argument to stored procedure
_bstr_t bstrProc=( L"storeProcedureName" ); //Stored procedure name
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2(DISP_E_PARAMNOTFOUND, VT_ERROR);
pCOMBUFFER->m_pCommand->ActiveConnection = m_pConn;
pCOMBUFFER->m_pCommand->CommandText = _bstr_t( bstrProc ); // passing the stored procedue
pCOMBUFFER->m_pCommand->CommandType = adCmdStoredProc; // type
pCOMBUFFER->m_pCommand->Parameters->Refresh(); // passing string value as argument to stored procedure
m_pCommand->Parameters->Item[ _variant_t( (long) 1 ) ]->Value =
_variant_t( (LPCTSTR)mParam1);
m_pRecordset = m_pCommand->Execute( &vtEmpty, &vtEmpty2, adCmdStoredProc );
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
mPwd=varTemp.bstrVal;
m_pRecordset->Close();
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
}
m_pConn->Close();
#2
HRESULT hr;
CString strDBSource;
_ConnectionPtr m_pConn;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;
//SQL Server数据库的连接串
strDBSource="Provider=SQLOLEDB.1;Password=abcde;Persist Security Info=True;User ID=sa;Initial Catalog=YourDataBaseName;Data Source=.;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=xxx;Use Encryption for Data=False;Tag with column collat"
_bstr_t strCnn(strDBSource);
try{
hr=m_pConn.CreateInstance("ADODB.Connection");
if(FAILED(hr)){
return FALSE;
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
return FALSE;
}
m_pConn->ConnectionTimeout=15;
m_pConn->Mode=adModeReadWrite;
m_pConn->IsolationLevel=adXactIsolated;
m_pConn->Attributes&=!adXactCommitRetaining;
m_pConn->Attributes&=!adXactAbortRetaining;
try{
m_pConn->Open(strCnn,"","",NULL);
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
return FALSE;
}catch(...){
return FALSE;
}
try{
m_pCommand.CreateInstance("ADODB.Command" ); // creating command object
} catch(...){
return false;
}
try{
hr=m_pRecordset.CreateInstance("ADODB.Recordset");
}catch(...){
return false;
}
//Recordset例子
_variant_t vConn;
_variant_t sqlStr=_T("select * from table1");
vConn=(IDispatch*) m_pConn;
try{
hr=m_pRecordset->Open(sqlStr,vConn,adOpenStatic,adLockOptimistic,-1);
if(!FAILED(hr)){
while (!m_pRecordset->EndOfFile){
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
CString mValue=varTemp.bstrVal;
m_pRecordset->MoveNext();
}
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE("%s\n",(LPCSTR) bstrDescription);
}catch(...){
}
m_pRecordset->Close();
//Command例子
try{//
passing string value as argument to stored procedure
_bstr_t bstrProc=( L"storeProcedureName" ); //Stored procedure name
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2(DISP_E_PARAMNOTFOUND, VT_ERROR);
pCOMBUFFER->m_pCommand->ActiveConnection = m_pConn;
pCOMBUFFER->m_pCommand->CommandText = _bstr_t( bstrProc ); // passing the stored procedue
pCOMBUFFER->m_pCommand->CommandType = adCmdStoredProc; // type
pCOMBUFFER->m_pCommand->Parameters->Refresh(); // passing string value as argument to stored procedure
m_pCommand->Parameters->Item[ _variant_t( (long) 1 ) ]->Value =
_variant_t( (LPCTSTR)mParam1);
m_pRecordset = m_pCommand->Execute( &vtEmpty, &vtEmpty2, adCmdStoredProc );
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
mPwd=varTemp.bstrVal;
m_pRecordset->Close();
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
}
m_pConn->Close();
#3
HRESULT hr;
CString strDBSource;
_ConnectionPtr m_pConn;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;
//SQL Server数据库的连接串
strDBSource="Provider=SQLOLEDB.1;Password=abcde;Persist Security Info=True;User ID=sa;Initial Catalog=YourDataBaseName;Data Source=.;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=xxx;Use Encryption for Data=False;Tag with column collat"
_bstr_t strCnn(strDBSource);
try{
hr=m_pConn.CreateInstance("ADODB.Connection");
if(FAILED(hr)){
return FALSE;
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
return FALSE;
}
m_pConn->ConnectionTimeout=15;
m_pConn->Mode=adModeReadWrite;
m_pConn->IsolationLevel=adXactIsolated;
m_pConn->Attributes&=!adXactCommitRetaining;
m_pConn->Attributes&=!adXactAbortRetaining;
try{
m_pConn->Open(strCnn,"","",NULL);
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
return FALSE;
}catch(...){
return FALSE;
}
try{
m_pCommand.CreateInstance("ADODB.Command" ); // creating command object
} catch(...){
return false;
}
try{
hr=m_pRecordset.CreateInstance("ADODB.Recordset");
}catch(...){
return false;
}
//Recordset例子
_variant_t vConn;
_variant_t sqlStr=_T("select * from table1");
vConn=(IDispatch*) m_pConn;
try{
hr=m_pRecordset->Open(sqlStr,vConn,adOpenStatic,adLockOptimistic,-1);
if(!FAILED(hr)){
while (!m_pRecordset->EndOfFile){
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
CString mValue=varTemp.bstrVal;
m_pRecordset->MoveNext();
}
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE("%s\n",(LPCSTR) bstrDescription);
}catch(...){
}
m_pRecordset->Close();
//Command例子
try{//
passing string value as argument to stored procedure
_bstr_t bstrProc=( L"storeProcedureName" ); //Stored procedure name
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2(DISP_E_PARAMNOTFOUND, VT_ERROR);
pCOMBUFFER->m_pCommand->ActiveConnection = m_pConn;
pCOMBUFFER->m_pCommand->CommandText = _bstr_t( bstrProc ); // passing the stored procedue
pCOMBUFFER->m_pCommand->CommandType = adCmdStoredProc; // type
pCOMBUFFER->m_pCommand->Parameters->Refresh(); // passing string value as argument to stored procedure
m_pCommand->Parameters->Item[ _variant_t( (long) 1 ) ]->Value =
_variant_t( (LPCTSTR)mParam1);
m_pRecordset = m_pCommand->Execute( &vtEmpty, &vtEmpty2, adCmdStoredProc );
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
mPwd=varTemp.bstrVal;
m_pRecordset->Close();
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
}
m_pConn->Close();
#4
UID=
username;Password=
password
就是这个格式。
就是这个格式。
#5
发错了,应该是:
ID= username;Password= password
ID= username;Password= password
#6
晕死了,又发错了,今晚怎么了....
应该是:
User ID= username;Password= password
应该是:
User ID= username;Password= password
#7
HRESULT hr;
CString strDBSource;
_ConnectionPtr m_pConn;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;
//SQL Server数据库的连接串
strDBSource="Provider=SQLOLEDB.1;Password=abcde;Persist Security Info=True;User ID=sa;Initial Catalog=YourDataBaseName;Data Source=.;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=xxx;Use Encryption for Data=False;Tag with column collat"
_bstr_t strCnn(strDBSource);
try{
hr=m_pConn.CreateInstance("ADODB.Connection");
if(FAILED(hr)){
return FALSE;
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
return FALSE;
}
m_pConn->ConnectionTimeout=15;
m_pConn->Mode=adModeReadWrite;
m_pConn->IsolationLevel=adXactIsolated;
m_pConn->Attributes&=!adXactCommitRetaining;
m_pConn->Attributes&=!adXactAbortRetaining;
try{
m_pConn->Open(strCnn,"","",NULL);
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
return FALSE;
}catch(...){
return FALSE;
}
try{
m_pCommand.CreateInstance("ADODB.Command" ); // creating command object
} catch(...){
return false;
}
try{
hr=m_pRecordset.CreateInstance("ADODB.Recordset");
}catch(...){
return false;
}
//Recordset例子
_variant_t vConn;
_variant_t sqlStr=_T("select * from table1");
vConn=(IDispatch*) m_pConn;
try{
hr=m_pRecordset->Open(sqlStr,vConn,adOpenStatic,adLockOptimistic,-1);
if(!FAILED(hr)){
while (!m_pRecordset->EndOfFile){
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
CString mValue=varTemp.bstrVal;
m_pRecordset->MoveNext();
}
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE("%s\n",(LPCSTR) bstrDescription);
}catch(...){
}
m_pRecordset->Close();
//Command例子
try{//
passing string value as argument to stored procedure
_bstr_t bstrProc=( L"storeProcedureName" ); //Stored procedure name
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2(DISP_E_PARAMNOTFOUND, VT_ERROR);
pCOMBUFFER->m_pCommand->ActiveConnection = m_pConn;
pCOMBUFFER->m_pCommand->CommandText = _bstr_t( bstrProc ); // passing the stored procedue
pCOMBUFFER->m_pCommand->CommandType = adCmdStoredProc; // type
pCOMBUFFER->m_pCommand->Parameters->Refresh(); // passing string value as argument to stored procedure
m_pCommand->Parameters->Item[ _variant_t( (long) 1 ) ]->Value =
_variant_t( (LPCTSTR)mParam1);
m_pRecordset = m_pCommand->Execute( &vtEmpty, &vtEmpty2, adCmdStoredProc );
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
mPwd=varTemp.bstrVal;
m_pRecordset->Close();
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
}
m_pConn->Close();
#8
HRESULT hr;
CString strDBSource;
_ConnectionPtr m_pConn;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;
//SQL Server数据库的连接串
strDBSource="Provider=SQLOLEDB.1;Password=abcde;Persist Security Info=True;User ID=sa;Initial Catalog=YourDataBaseName;Data Source=.;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=xxx;Use Encryption for Data=False;Tag with column collat"
_bstr_t strCnn(strDBSource);
try{
hr=m_pConn.CreateInstance("ADODB.Connection");
if(FAILED(hr)){
return FALSE;
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
return FALSE;
}
m_pConn->ConnectionTimeout=15;
m_pConn->Mode=adModeReadWrite;
m_pConn->IsolationLevel=adXactIsolated;
m_pConn->Attributes&=!adXactCommitRetaining;
m_pConn->Attributes&=!adXactAbortRetaining;
try{
m_pConn->Open(strCnn,"","",NULL);
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
return FALSE;
}catch(...){
return FALSE;
}
try{
m_pCommand.CreateInstance("ADODB.Command" ); // creating command object
} catch(...){
return false;
}
try{
hr=m_pRecordset.CreateInstance("ADODB.Recordset");
}catch(...){
return false;
}
//Recordset例子
_variant_t vConn;
_variant_t sqlStr=_T("select * from table1");
vConn=(IDispatch*) m_pConn;
try{
hr=m_pRecordset->Open(sqlStr,vConn,adOpenStatic,adLockOptimistic,-1);
if(!FAILED(hr)){
while (!m_pRecordset->EndOfFile){
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
CString mValue=varTemp.bstrVal;
m_pRecordset->MoveNext();
}
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE("%s\n",(LPCSTR) bstrDescription);
}catch(...){
}
m_pRecordset->Close();
//Command例子
try{//
passing string value as argument to stored procedure
_bstr_t bstrProc=( L"storeProcedureName" ); //Stored procedure name
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2(DISP_E_PARAMNOTFOUND, VT_ERROR);
pCOMBUFFER->m_pCommand->ActiveConnection = m_pConn;
pCOMBUFFER->m_pCommand->CommandText = _bstr_t( bstrProc ); // passing the stored procedue
pCOMBUFFER->m_pCommand->CommandType = adCmdStoredProc; // type
pCOMBUFFER->m_pCommand->Parameters->Refresh(); // passing string value as argument to stored procedure
m_pCommand->Parameters->Item[ _variant_t( (long) 1 ) ]->Value =
_variant_t( (LPCTSTR)mParam1);
m_pRecordset = m_pCommand->Execute( &vtEmpty, &vtEmpty2, adCmdStoredProc );
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
mPwd=varTemp.bstrVal;
m_pRecordset->Close();
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
}
m_pConn->Close();
#9
“我连接的时候把 LoginDome.mdf 这个数据库文件和日志文件LoginDemo_log.ldf放在工程下面。
然后代码中用上面的语句访问”,
就是这个原因,这又不是access数据库,你不用管目录,Sql Server自己会管理的,你只要给出服务器名称、数据库名称,有密码设置的给出密码!
你把连接字符串改成这样:
"Provider=SQLOLEDB;Server=(local);Database=LoginDemo;Uid=sa; PWD= "
然后代码中用上面的语句访问”,
就是这个原因,这又不是access数据库,你不用管目录,Sql Server自己会管理的,你只要给出服务器名称、数据库名称,有密码设置的给出密码!
你把连接字符串改成这样:
"Provider=SQLOLEDB;Server=(local);Database=LoginDemo;Uid=sa; PWD= "
#10
我的代码大致是这样的:
CString strConnectionStr;
strConnectionStr.Format(_T("driver={SQL Server};Server=%s,1433;DATABASE=%s;UID=%s;PWD=%s")
,g_strIP
,g_strDB
,g_strName
,g_strPWD
);
CoInitializeEx( NULL, COINIT_MULTITHREADED );
HRESULT hr = E_FAIL;
try
{
if(NULL == m_pConnPtr )
{
hr = m_pConnPtr.CreateInstance(__uuidof(Connection));
if (FAILED(hr)) //创建失败
return hr;
}
//连接状态
if(m_pConnPtr->State)
{
m_pConnPtr->Close ();
}
hr = S_FALSE;
//打开数据库
hr = m_pConnPtr->Open(_bstr_t(strConnectionStr),
_T(""),_T(""),
adConnectUnspecified);
if (hr == S_OK)
{
m_pCommandPtr.CreateInstance(__uuidof(Command));
m_pCommandPtr->ActiveConnection = m_pConnPtr;
}
}catch(...){
hr = S_FALSE;
}
return hr;
#11
我连接的时候把 LoginDome.mdf 这个数据库文件和日志文件LoginDemo_log.ldf放在工程下面。
看文件时sqlserver 数据库,要先把文件附加到sqlserver 服务器,再进行操作
看文件时sqlserver 数据库,要先把文件附加到sqlserver 服务器,再进行操作
#12
DSN=%s;UID=;PWD=
#13
对了,如果用OLDDB,查询效率会更高很多。
大概是ODBC的5-10倍
---->> 耗时:0.891s
共筛选出:5584 条记录
ODBC
---->> 耗时:8.750s
共筛选出:5584 条记录
大概是ODBC的5-10倍
strConnectionStr.Format(_T("Provider=SQLOLEDB;Server=%s;Database=%s; uid=%s; pwd=%s;")
,g_strIP
,g_strDB
,g_strName
,g_strPWD
);
---->> 耗时:0.891s
共筛选出:5584 条记录
ODBC
strConnectionStr.Format(_T("driver={SQL Server};Server=%s,1433;DATABASE=%s;UID=%s;PWD=%s")
,g_strIP
,g_strDB
,g_strName
,g_strPWD
);
---->> 耗时:8.750s
共筛选出:5584 条记录
#14
一下是查询语句测试:
---------------------------
启动<<查 询>>服务
---------------------------
1. 查询记录
Select ZhLGD.Name,Route.Name,[1ZhChD].Name,Wafer.Name,PQS.Name,[10010].[ID_Oper],[10010].[DT_Oper],[10010].[ID_MoveOut],[10010].[DT_MoveOut] From ZhLGD,[10010],Route,[1ZhChD],Wafer,PQS Where [10010].[ID_ZhLGD] = ZhLGD.[ID_ZhLGD] and [10010].[ID_Route] = Route.[ID_Route] and [10010].[ID_ZhChD] = [1ZhChD].[ID_ZhChD] and [10010].[ID_Wafer] = Wafer.[ID_Wafer] and [10010].[ID_PQS] = PQS.[ID_PQS] ORDER BY [1ZhChD].Name ASC ,[Wafer].Name ASC
---->> 耗时:0.891s
共筛选出:5584 条记录
---->> 耗时:0.406s
---------------------------
启动<<查 询>>服务
---------------------------
1. 查询记录
Select ZhLGD.Name,Route.Name,[1ZhChD].Name,Wafer.Name,PQS.Name,[10010].[ID_Oper],[10010].[DT_Oper],[10010].[ID_MoveOut],[10010].[DT_MoveOut] From ZhLGD,[10010],Route,[1ZhChD],Wafer,PQS Where [10010].[ID_ZhLGD] = ZhLGD.[ID_ZhLGD] and [10010].[ID_Route] = Route.[ID_Route] and [10010].[ID_ZhChD] = [1ZhChD].[ID_ZhChD] and [10010].[ID_Wafer] = Wafer.[ID_Wafer] and [10010].[ID_PQS] = PQS.[ID_PQS] ORDER BY [1ZhChD].Name ASC ,[Wafer].Name ASC
---->> 耗时:0.891s
共筛选出:5584 条记录
---->> 耗时:0.406s
#15
ID=username;Password=password
#1
HRESULT hr;
CString strDBSource;
_ConnectionPtr m_pConn;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;
//SQL Server数据库的连接串
strDBSource="Provider=SQLOLEDB.1;Password=abcde;Persist Security Info=True;User ID=sa;Initial Catalog=wzWebLab;Data Source=.;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=LIUZG;Use Encryption for Data=False;Tag with column collat"
_bstr_t strCnn(strDBSource);
try{
hr=m_pConn.CreateInstance("ADODB.Connection");
if(FAILED(hr)){
return FALSE;
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
return FALSE;
}
m_pConn->ConnectionTimeout=15;
m_pConn->Mode=adModeReadWrite;
m_pConn->IsolationLevel=adXactIsolated;
m_pConn->Attributes&=!adXactCommitRetaining;
m_pConn->Attributes&=!adXactAbortRetaining;
try{
m_pConn->Open(strCnn,"","",NULL);
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
return FALSE;
}catch(...){
return FALSE;
}
try{
m_pCommand.CreateInstance("ADODB.Command" ); // creating command object
} catch(...){
return false;
}
try{
hr=m_pRecordset.CreateInstance("ADODB.Recordset");
}catch(...){
return false;
}
//Recordset例子
_variant_t vConn;
_variant_t sqlStr=_T("select * from table1");
vConn=(IDispatch*) m_pConn;
try{
hr=m_pRecordset->Open(sqlStr,vConn,adOpenStatic,adLockOptimistic,-1);
if(!FAILED(hr)){
while (!m_pRecordset->EndOfFile){
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
CString mValue=varTemp.bstrVal;
m_pRecordset->MoveNext();
}
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE("%s\n",(LPCSTR) bstrDescription);
}catch(...){
}
m_pRecordset->Close();
//Command例子
try{//
passing string value as argument to stored procedure
_bstr_t bstrProc=( L"storeProcedureName" ); //Stored procedure name
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2(DISP_E_PARAMNOTFOUND, VT_ERROR);
pCOMBUFFER->m_pCommand->ActiveConnection = m_pConn;
pCOMBUFFER->m_pCommand->CommandText = _bstr_t( bstrProc ); // passing the stored procedue
pCOMBUFFER->m_pCommand->CommandType = adCmdStoredProc; // type
pCOMBUFFER->m_pCommand->Parameters->Refresh(); // passing string value as argument to stored procedure
m_pCommand->Parameters->Item[ _variant_t( (long) 1 ) ]->Value =
_variant_t( (LPCTSTR)mParam1);
m_pRecordset = m_pCommand->Execute( &vtEmpty, &vtEmpty2, adCmdStoredProc );
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
mPwd=varTemp.bstrVal;
m_pRecordset->Close();
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
}
m_pConn->Close();
#2
HRESULT hr;
CString strDBSource;
_ConnectionPtr m_pConn;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;
//SQL Server数据库的连接串
strDBSource="Provider=SQLOLEDB.1;Password=abcde;Persist Security Info=True;User ID=sa;Initial Catalog=YourDataBaseName;Data Source=.;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=xxx;Use Encryption for Data=False;Tag with column collat"
_bstr_t strCnn(strDBSource);
try{
hr=m_pConn.CreateInstance("ADODB.Connection");
if(FAILED(hr)){
return FALSE;
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
return FALSE;
}
m_pConn->ConnectionTimeout=15;
m_pConn->Mode=adModeReadWrite;
m_pConn->IsolationLevel=adXactIsolated;
m_pConn->Attributes&=!adXactCommitRetaining;
m_pConn->Attributes&=!adXactAbortRetaining;
try{
m_pConn->Open(strCnn,"","",NULL);
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
return FALSE;
}catch(...){
return FALSE;
}
try{
m_pCommand.CreateInstance("ADODB.Command" ); // creating command object
} catch(...){
return false;
}
try{
hr=m_pRecordset.CreateInstance("ADODB.Recordset");
}catch(...){
return false;
}
//Recordset例子
_variant_t vConn;
_variant_t sqlStr=_T("select * from table1");
vConn=(IDispatch*) m_pConn;
try{
hr=m_pRecordset->Open(sqlStr,vConn,adOpenStatic,adLockOptimistic,-1);
if(!FAILED(hr)){
while (!m_pRecordset->EndOfFile){
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
CString mValue=varTemp.bstrVal;
m_pRecordset->MoveNext();
}
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE("%s\n",(LPCSTR) bstrDescription);
}catch(...){
}
m_pRecordset->Close();
//Command例子
try{//
passing string value as argument to stored procedure
_bstr_t bstrProc=( L"storeProcedureName" ); //Stored procedure name
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2(DISP_E_PARAMNOTFOUND, VT_ERROR);
pCOMBUFFER->m_pCommand->ActiveConnection = m_pConn;
pCOMBUFFER->m_pCommand->CommandText = _bstr_t( bstrProc ); // passing the stored procedue
pCOMBUFFER->m_pCommand->CommandType = adCmdStoredProc; // type
pCOMBUFFER->m_pCommand->Parameters->Refresh(); // passing string value as argument to stored procedure
m_pCommand->Parameters->Item[ _variant_t( (long) 1 ) ]->Value =
_variant_t( (LPCTSTR)mParam1);
m_pRecordset = m_pCommand->Execute( &vtEmpty, &vtEmpty2, adCmdStoredProc );
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
mPwd=varTemp.bstrVal;
m_pRecordset->Close();
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
}
m_pConn->Close();
#3
HRESULT hr;
CString strDBSource;
_ConnectionPtr m_pConn;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;
//SQL Server数据库的连接串
strDBSource="Provider=SQLOLEDB.1;Password=abcde;Persist Security Info=True;User ID=sa;Initial Catalog=YourDataBaseName;Data Source=.;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=xxx;Use Encryption for Data=False;Tag with column collat"
_bstr_t strCnn(strDBSource);
try{
hr=m_pConn.CreateInstance("ADODB.Connection");
if(FAILED(hr)){
return FALSE;
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
return FALSE;
}
m_pConn->ConnectionTimeout=15;
m_pConn->Mode=adModeReadWrite;
m_pConn->IsolationLevel=adXactIsolated;
m_pConn->Attributes&=!adXactCommitRetaining;
m_pConn->Attributes&=!adXactAbortRetaining;
try{
m_pConn->Open(strCnn,"","",NULL);
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
return FALSE;
}catch(...){
return FALSE;
}
try{
m_pCommand.CreateInstance("ADODB.Command" ); // creating command object
} catch(...){
return false;
}
try{
hr=m_pRecordset.CreateInstance("ADODB.Recordset");
}catch(...){
return false;
}
//Recordset例子
_variant_t vConn;
_variant_t sqlStr=_T("select * from table1");
vConn=(IDispatch*) m_pConn;
try{
hr=m_pRecordset->Open(sqlStr,vConn,adOpenStatic,adLockOptimistic,-1);
if(!FAILED(hr)){
while (!m_pRecordset->EndOfFile){
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
CString mValue=varTemp.bstrVal;
m_pRecordset->MoveNext();
}
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE("%s\n",(LPCSTR) bstrDescription);
}catch(...){
}
m_pRecordset->Close();
//Command例子
try{//
passing string value as argument to stored procedure
_bstr_t bstrProc=( L"storeProcedureName" ); //Stored procedure name
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2(DISP_E_PARAMNOTFOUND, VT_ERROR);
pCOMBUFFER->m_pCommand->ActiveConnection = m_pConn;
pCOMBUFFER->m_pCommand->CommandText = _bstr_t( bstrProc ); // passing the stored procedue
pCOMBUFFER->m_pCommand->CommandType = adCmdStoredProc; // type
pCOMBUFFER->m_pCommand->Parameters->Refresh(); // passing string value as argument to stored procedure
m_pCommand->Parameters->Item[ _variant_t( (long) 1 ) ]->Value =
_variant_t( (LPCTSTR)mParam1);
m_pRecordset = m_pCommand->Execute( &vtEmpty, &vtEmpty2, adCmdStoredProc );
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
mPwd=varTemp.bstrVal;
m_pRecordset->Close();
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
}
m_pConn->Close();
#4
UID=
username;Password=
password
就是这个格式。
就是这个格式。
#5
发错了,应该是:
ID= username;Password= password
ID= username;Password= password
#6
晕死了,又发错了,今晚怎么了....
应该是:
User ID= username;Password= password
应该是:
User ID= username;Password= password
#7
HRESULT hr;
CString strDBSource;
_ConnectionPtr m_pConn;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;
//SQL Server数据库的连接串
strDBSource="Provider=SQLOLEDB.1;Password=abcde;Persist Security Info=True;User ID=sa;Initial Catalog=YourDataBaseName;Data Source=.;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=xxx;Use Encryption for Data=False;Tag with column collat"
_bstr_t strCnn(strDBSource);
try{
hr=m_pConn.CreateInstance("ADODB.Connection");
if(FAILED(hr)){
return FALSE;
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
return FALSE;
}
m_pConn->ConnectionTimeout=15;
m_pConn->Mode=adModeReadWrite;
m_pConn->IsolationLevel=adXactIsolated;
m_pConn->Attributes&=!adXactCommitRetaining;
m_pConn->Attributes&=!adXactAbortRetaining;
try{
m_pConn->Open(strCnn,"","",NULL);
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
return FALSE;
}catch(...){
return FALSE;
}
try{
m_pCommand.CreateInstance("ADODB.Command" ); // creating command object
} catch(...){
return false;
}
try{
hr=m_pRecordset.CreateInstance("ADODB.Recordset");
}catch(...){
return false;
}
//Recordset例子
_variant_t vConn;
_variant_t sqlStr=_T("select * from table1");
vConn=(IDispatch*) m_pConn;
try{
hr=m_pRecordset->Open(sqlStr,vConn,adOpenStatic,adLockOptimistic,-1);
if(!FAILED(hr)){
while (!m_pRecordset->EndOfFile){
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
CString mValue=varTemp.bstrVal;
m_pRecordset->MoveNext();
}
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE("%s\n",(LPCSTR) bstrDescription);
}catch(...){
}
m_pRecordset->Close();
//Command例子
try{//
passing string value as argument to stored procedure
_bstr_t bstrProc=( L"storeProcedureName" ); //Stored procedure name
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2(DISP_E_PARAMNOTFOUND, VT_ERROR);
pCOMBUFFER->m_pCommand->ActiveConnection = m_pConn;
pCOMBUFFER->m_pCommand->CommandText = _bstr_t( bstrProc ); // passing the stored procedue
pCOMBUFFER->m_pCommand->CommandType = adCmdStoredProc; // type
pCOMBUFFER->m_pCommand->Parameters->Refresh(); // passing string value as argument to stored procedure
m_pCommand->Parameters->Item[ _variant_t( (long) 1 ) ]->Value =
_variant_t( (LPCTSTR)mParam1);
m_pRecordset = m_pCommand->Execute( &vtEmpty, &vtEmpty2, adCmdStoredProc );
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
mPwd=varTemp.bstrVal;
m_pRecordset->Close();
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
}
m_pConn->Close();
#8
HRESULT hr;
CString strDBSource;
_ConnectionPtr m_pConn;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;
//SQL Server数据库的连接串
strDBSource="Provider=SQLOLEDB.1;Password=abcde;Persist Security Info=True;User ID=sa;Initial Catalog=YourDataBaseName;Data Source=.;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=xxx;Use Encryption for Data=False;Tag with column collat"
_bstr_t strCnn(strDBSource);
try{
hr=m_pConn.CreateInstance("ADODB.Connection");
if(FAILED(hr)){
return FALSE;
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
return FALSE;
}
m_pConn->ConnectionTimeout=15;
m_pConn->Mode=adModeReadWrite;
m_pConn->IsolationLevel=adXactIsolated;
m_pConn->Attributes&=!adXactCommitRetaining;
m_pConn->Attributes&=!adXactAbortRetaining;
try{
m_pConn->Open(strCnn,"","",NULL);
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
return FALSE;
}catch(...){
return FALSE;
}
try{
m_pCommand.CreateInstance("ADODB.Command" ); // creating command object
} catch(...){
return false;
}
try{
hr=m_pRecordset.CreateInstance("ADODB.Recordset");
}catch(...){
return false;
}
//Recordset例子
_variant_t vConn;
_variant_t sqlStr=_T("select * from table1");
vConn=(IDispatch*) m_pConn;
try{
hr=m_pRecordset->Open(sqlStr,vConn,adOpenStatic,adLockOptimistic,-1);
if(!FAILED(hr)){
while (!m_pRecordset->EndOfFile){
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
CString mValue=varTemp.bstrVal;
m_pRecordset->MoveNext();
}
}
}catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE("%s\n",(LPCSTR) bstrDescription);
}catch(...){
}
m_pRecordset->Close();
//Command例子
try{//
passing string value as argument to stored procedure
_bstr_t bstrProc=( L"storeProcedureName" ); //Stored procedure name
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2(DISP_E_PARAMNOTFOUND, VT_ERROR);
pCOMBUFFER->m_pCommand->ActiveConnection = m_pConn;
pCOMBUFFER->m_pCommand->CommandText = _bstr_t( bstrProc ); // passing the stored procedue
pCOMBUFFER->m_pCommand->CommandType = adCmdStoredProc; // type
pCOMBUFFER->m_pCommand->Parameters->Refresh(); // passing string value as argument to stored procedure
m_pCommand->Parameters->Item[ _variant_t( (long) 1 ) ]->Value =
_variant_t( (LPCTSTR)mParam1);
m_pRecordset = m_pCommand->Execute( &vtEmpty, &vtEmpty2, adCmdStoredProc );
VARIANT varTemp;
varTemp=m_pRecordset->Fields->Item["FldName"]->Value;
mPwd=varTemp.bstrVal;
m_pRecordset->Close();
}
catch(_com_error &e){
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
}catch(...){
}
m_pConn->Close();
#9
“我连接的时候把 LoginDome.mdf 这个数据库文件和日志文件LoginDemo_log.ldf放在工程下面。
然后代码中用上面的语句访问”,
就是这个原因,这又不是access数据库,你不用管目录,Sql Server自己会管理的,你只要给出服务器名称、数据库名称,有密码设置的给出密码!
你把连接字符串改成这样:
"Provider=SQLOLEDB;Server=(local);Database=LoginDemo;Uid=sa; PWD= "
然后代码中用上面的语句访问”,
就是这个原因,这又不是access数据库,你不用管目录,Sql Server自己会管理的,你只要给出服务器名称、数据库名称,有密码设置的给出密码!
你把连接字符串改成这样:
"Provider=SQLOLEDB;Server=(local);Database=LoginDemo;Uid=sa; PWD= "
#10
我的代码大致是这样的:
CString strConnectionStr;
strConnectionStr.Format(_T("driver={SQL Server};Server=%s,1433;DATABASE=%s;UID=%s;PWD=%s")
,g_strIP
,g_strDB
,g_strName
,g_strPWD
);
CoInitializeEx( NULL, COINIT_MULTITHREADED );
HRESULT hr = E_FAIL;
try
{
if(NULL == m_pConnPtr )
{
hr = m_pConnPtr.CreateInstance(__uuidof(Connection));
if (FAILED(hr)) //创建失败
return hr;
}
//连接状态
if(m_pConnPtr->State)
{
m_pConnPtr->Close ();
}
hr = S_FALSE;
//打开数据库
hr = m_pConnPtr->Open(_bstr_t(strConnectionStr),
_T(""),_T(""),
adConnectUnspecified);
if (hr == S_OK)
{
m_pCommandPtr.CreateInstance(__uuidof(Command));
m_pCommandPtr->ActiveConnection = m_pConnPtr;
}
}catch(...){
hr = S_FALSE;
}
return hr;
#11
我连接的时候把 LoginDome.mdf 这个数据库文件和日志文件LoginDemo_log.ldf放在工程下面。
看文件时sqlserver 数据库,要先把文件附加到sqlserver 服务器,再进行操作
看文件时sqlserver 数据库,要先把文件附加到sqlserver 服务器,再进行操作
#12
DSN=%s;UID=;PWD=
#13
对了,如果用OLDDB,查询效率会更高很多。
大概是ODBC的5-10倍
---->> 耗时:0.891s
共筛选出:5584 条记录
ODBC
---->> 耗时:8.750s
共筛选出:5584 条记录
大概是ODBC的5-10倍
strConnectionStr.Format(_T("Provider=SQLOLEDB;Server=%s;Database=%s; uid=%s; pwd=%s;")
,g_strIP
,g_strDB
,g_strName
,g_strPWD
);
---->> 耗时:0.891s
共筛选出:5584 条记录
ODBC
strConnectionStr.Format(_T("driver={SQL Server};Server=%s,1433;DATABASE=%s;UID=%s;PWD=%s")
,g_strIP
,g_strDB
,g_strName
,g_strPWD
);
---->> 耗时:8.750s
共筛选出:5584 条记录
#14
一下是查询语句测试:
---------------------------
启动<<查 询>>服务
---------------------------
1. 查询记录
Select ZhLGD.Name,Route.Name,[1ZhChD].Name,Wafer.Name,PQS.Name,[10010].[ID_Oper],[10010].[DT_Oper],[10010].[ID_MoveOut],[10010].[DT_MoveOut] From ZhLGD,[10010],Route,[1ZhChD],Wafer,PQS Where [10010].[ID_ZhLGD] = ZhLGD.[ID_ZhLGD] and [10010].[ID_Route] = Route.[ID_Route] and [10010].[ID_ZhChD] = [1ZhChD].[ID_ZhChD] and [10010].[ID_Wafer] = Wafer.[ID_Wafer] and [10010].[ID_PQS] = PQS.[ID_PQS] ORDER BY [1ZhChD].Name ASC ,[Wafer].Name ASC
---->> 耗时:0.891s
共筛选出:5584 条记录
---->> 耗时:0.406s
---------------------------
启动<<查 询>>服务
---------------------------
1. 查询记录
Select ZhLGD.Name,Route.Name,[1ZhChD].Name,Wafer.Name,PQS.Name,[10010].[ID_Oper],[10010].[DT_Oper],[10010].[ID_MoveOut],[10010].[DT_MoveOut] From ZhLGD,[10010],Route,[1ZhChD],Wafer,PQS Where [10010].[ID_ZhLGD] = ZhLGD.[ID_ZhLGD] and [10010].[ID_Route] = Route.[ID_Route] and [10010].[ID_ZhChD] = [1ZhChD].[ID_ZhChD] and [10010].[ID_Wafer] = Wafer.[ID_Wafer] and [10010].[ID_PQS] = PQS.[ID_PQS] ORDER BY [1ZhChD].Name ASC ,[Wafer].Name ASC
---->> 耗时:0.891s
共筛选出:5584 条记录
---->> 耗时:0.406s
#15
ID=username;Password=password