Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-ServerMFCVC++
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 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  
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..

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 576
1 Tadit Dash 215
2 Sergey Alexandrovich Kryukov 190
3 OriginalGriff 183
4 Peter Leow 130
0 Sergey Alexandrovich Kryukov 9,325
1 OriginalGriff 5,473
2 Peter Leow 4,150
3 Maciej Los 3,540
4 Abhinav S 3,333


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