VC ++和SQL Server之间的ODBC连接问题 [英] Problem in ODBC connectivity between VC++ and SQL Server
本文介绍了VC ++和SQL Server之间的ODBC连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经编写了以下用于连接的代码
I have written following code for connectivity
CDatabase database;
CString SqlString;
CString ssn,firstname;
CString sDriver = _T("SQL Server");
CString sDsn, sMc;
sMc.Format(_T("MERILDSK33"));
CString sFile = _T("C:\\Program Files\\Microsoft SQL Server\\MSSQL10.MSSQLSERVER\\MSSQL\\DATA\\DBTrial.mdf");
sDsn.Format(_T("ODBC;DRIVER={%s};Server=%s;Database=%s;Trusted_Connection=yes"),sDriver,sMc,sFile);
// You must change above path if it's different
int iRec = 0;
try
{
// Open the database
database.Open(NULL,false,false,sDsn);
// Allocate the recordset
CRecordset recset(&database);
// Build the SQL statement
SqlString = _T("select * from dbo.Person");
// Execute the query
recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
// Reset List control if there is any data
while( !recset.IsEOF() )
{
recset.GetFieldValue(_T("SSN"),ssn);
recset.GetFieldValue(_T("PersonFN"),firstname);
recset.MoveNext();
}
database.Close();
}
catch(CDBException* e)
{
AfxMessageBox(e->m_strError);
}
但是在database.open行上,出现以下错误
But on database.open line I get following error
Connection failed SQL State: ''08004'' SQL Server Error:4060 Server rejected the connection;Access to selected database has been denied
我试图以管理员身份运行,但仍然出现错误,服务器名称,mdf文件名和路径都正确.服务器也处于运行状态.我正在使用的用户与SQL Server的窗口身份验证相同.我不知道该怎么办?
请帮助
I tried to run as administrator,but still error comes ,Server name,mdf file name and path everything is correct.Server is also in running condition.The user in which I am ruuning is same as window Authantication of SQL server.I dont know what to do?
Please help
推荐答案
^ ]
检查此链接..我认为这是您遇到的相同问题...让我知道您是否知道...
另请参阅此链接
http://www.utteraccess.com/forum/lofiversion/index.php/t1979711.html [^ ]
http://social.msdn.microsoft.com/Forums/en/sqldatabaseengine/thread/33d08813-e447-401e-9a60-ce4af5484458[^]
Check this link..i think it''s the same problem u got...let me know u got or not...
see this link also
http://www.utteraccess.com/forum/lofiversion/index.php/t1979711.html[^]
在进行反复试验时,我找到了解决方法
以下连接字符串有效.
While doing trial and error I found solution
following connection string worked.
sDsn.Format( _T("DSN=Trial;Trusted_Connection=Yes;WSID=MERILDSK33;DATABASE=DBTrial"));
这篇关于VC ++和SQL Server之间的ODBC连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文