正定古城导游词10分钟:如何用VC++登录SQL Server数据库?

来源:百度文库 编辑:高校问答 时间:2024/04/26 08:09:14
我用VC++编了一个程序,登录数据时总是显示“用户登录失败,无效的连接字符串属性!”请问是数据源设置的问题还是程序的问题,如何能成功连接呢?谢谢!

g_pDBSource = new (CDatabase);
CString strLinkStr;
strLinkStr.Format("DSN=VISCTI;UID=vis;PWD=cti");
try
{
g_pDBSource->OpenEx(strLinkStr,CDatabase::noOdbcDialog);
}
catch(CDBException *e)
{
AfxMessageBox(e->m_strError);
e->Delete();
delete g_pDBSource;
return ;
}

取数据:
if ( g_pDBSource == NULL )
return -2;
CRecordset* rs= NULL ;
try
{
rs=new CRecordset( g_pDBSource );
CString strCount,strTime;
if ( rs->Open( CRecordset::snapshot,sql)!=0 )
{

while( !rs->IsEOF( ) )
{
rs->GetFieldValue((int)0,strCount);
nCount = atol(strCount);
rs->GetFieldValue((int)1,strTime);
switch(index)
{
case 0: //计算总数,构造结构
m_ItemInfo[Counter].strTime = strTime ;
m_ItemInfo[Counter].nCount0 = nCount ;
break;
case 1://填充结构
m_ItemInfo[this->SearchItem(strTime)].nCount1 = nCount ;
break;
case 2://填充结构
m_ItemInfo[this->SearchItem(strTime)].nCount2 = nCount ;
break;
case 3://填充结构
m_ItemInfo[this->SearchItem(strTime)].nCount3 = nCount ;
break;
}
Counter ++ ;

if ( index == 0 )
totalNum = Counter ;
rs->MoveNext();
}
}
else
{
delete rs ;
return FALSE ;
}
delete rs ;
rs = NULL ;
}
catch (CDBException* e )
{
AfxMessageBox(e->m_strError);
if ( rs != NULL ) delete rs ;
e->Delete () ;
return FALSE;
}
return TRUE ;
m_strCnn="Provider=MSDAORA.1;Password="+strPassWord+
";User ID="+strUser+
";Data Source="+strServerName;

m_pConnection->CursorLocation = adUseClient;
hr = m_pConnection->Open(
(_bstr_t)strCnn,
(_bstr_t)_T(""),
(_bstr_t)_T(""),
adModeUnknown
);
这样就建立了连接,用的是oracle数据库,用其他数据库,strCnn有点不同,自己去查一下