Click here to Skip to main content
Rate this: bad
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;
	CString sFile = _T("C:\\Program Files\\Microsoft SQL Server\\MSSQL10.MSSQLSERVER\\MSSQL\\DATA\\DBTrial.mdf");
	// You must change above path if it's different
	int iRec = 0; 	
		// Open the database
		// Allocate the recordset
		CRecordset recset(&database);
		// Build the SQL statement
		SqlString =  _T("select * from dbo.Person");
		// Execute the query
		// Reset List control if there is any data
		while( !recset.IsEOF() )
	catch(CDBException* e)
But on 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 20:52pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1[^]

Check this link..i think it's the same problem u got...let me know u got or not...
see this link also[^]
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
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"));

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

  Print Answers RSS
0 Mika Wendelius 161
1 Abhinav S 115
2 Peter Leow 110
3 Suvendu Shekhar Giri 100
4 Dave Kreskowiak 85
0 Sergey Alexandrovich Kryukov 8,513
1 OriginalGriff 6,526
2 Peter Leow 3,657
3 Zoltán Zörgő 3,426
4 Richard MacCutchan 2,417

Advertise | Privacy | Mobile
Web04 | 2.8.150123.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2015
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