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 19: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 Sergey Alexandrovich Kryukov 566
1 OriginalGriff 345
2 CPallini 200
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,028
2 CPallini 4,700
3 George Jonsson 3,142
4 Gihan Liyanage 2,450

Advertise | Privacy | Mobile
Web02 | 2.8.140916.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