Click here to Skip to main content
15,895,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have written following code for connectivity

C++
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

 
Share this answer
 
v2
Comments
adityarao31 8-Oct-12 3:39am    
Actually both of these links were not useful
[no name] 8-Oct-12 3:40am    
Ok..let me check..will update..
While doing trial and error I found solution
following connection string worked.

sDsn.Format( _T("DSN=Trial;Trusted_Connection=Yes;WSID=MERILDSK33;DATABASE=DBTrial"));
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900