Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server MFC VC++
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);
	}
 
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
 
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
Posted 7-Oct-12 19:52pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
v2
Comments
adityarao31 at 8-Oct-12 3:39am
   
Actually both of these links were not useful
snehasish nandy at 8-Oct-12 3:40am
   
Ok..let me check..will update..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

While doing trial and error I found solution
following connection string worked.
 
sDsn.Format( _T("DSN=Trial;Trusted_Connection=Yes;WSID=MERILDSK33;DATABASE=DBTrial"));
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Kornfeld Eliyahu Peter 430
1 OriginalGriff 304
2 Nelek 115
3 Sergey Alexandrovich Kryukov 99
4 Gihan Liyanage 99
0 Kornfeld Eliyahu Peter 420
1 OriginalGriff 140
2 Gihan Liyanage 99
3 Nelek 65
4 sankarsan parida 65


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100