CDatabase::ExecuteSQL() : Whats wrong with this code !?

Answered CDatabase::ExecuteSQL() : Whats wrong with this code !?

  • Saturday, February 16, 2013 2:54 AM
     
      Has Code

    	CDatabase masterdb;
     
    	masterdb.OpenEx(_T("DRIVER=SQL Server;DATABASE=master;Trusted_Connection=Yes;SERVER=(local)\\SQLEXPRESS"));
     
    	CString strCurrentDir;
     
    	::GetModuleFileName(NULL, strCurrentDir.GetBufferSetLength(_MAX_PATH), _MAX_PATH);
    	strCurrentDir.ReleaseBuffer();
    	strCurrentDir = strCurrentDir.Left(strCurrentDir.ReverseFind('\\') + 1);
     
    	CString strExec = _T("create database test on (name='test_data', filename = '") + strCurrentDir + _T("test_data.mdf')") +
    		                             _T("\nlog on (name='test_log', filename = '") + strCurrentDir + _T("test_log.ldf')");
    	
    	TRY
    	{
    		masterdb.ExecuteSQL(strExec);
    	}
    	CATCH(CDBException, e)
    	{
    		TCHAR buff[1024];
    		e->GetErrorMessage(buff, 1024);
    		AfxMessageBox(buff);
    		e->Delete();
    	}
    	END_CATCH
     
    	masterdb.Close();
    
     
    handled Exception when reaching masterdb.ExecuteSQL(strExec); says:
     

    CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'C:\Program Files (x86)\Microsoft Visual Studio\MyProjects\master\Debug\test_data.mdf'.
    CREATE DATABASE failed. Some file names listed could not be created.
    Check related errors.
    
     
    I don't know what it means.
     
    Help me please.
     
    Thank you for your understanding.


    • Edited by tomay3000 Sunday, February 17, 2013 2:08 AM
    •  

All Replies

  • Sunday, February 17, 2013 2:58 PM
     
     Answered
    Wrong is that normal programs have no read/write access in %PROGRAMFILES%, this also applies to a normal SQL Server installation. You should check which account is used by the SQL Server Windows service, then if it is a system account change it to a dedicated normal account and set the necessary permissions for this account. See Change the Service Startup Account for SQL Server (SQL Server Configuration Manager).
    • Marked As Answer by tomay3000 Monday, February 18, 2013 12:15 AM
    •  
  • Monday, February 18, 2013 12:19 AM
     
     
    You are right, I have moved the application EXE file to a non-system directory, & it has executed without problem (the file test_data.mdf & test_log.ldf has been created in the application EXE directory)