CDatabase::ExecuteSQL() : Whats wrong with this code !?
-
Saturday, February 16, 2013 2:54 AM
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();
A handled Exception when reachingmasterdb.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
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 AMYou 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)

