如何用VC++登录SQL Server数据库?

来源:百度知道 编辑:UC知道 时间:2024/05/13 09:28:05
我用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;