Click here to Skip to main content
13,589,789 members
Rate this:
Please Sign up or sign in to vote.
See more:
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 = 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')");

CATCH(CDBException, e)
    TCHAR buff[1024];
    e->GetErrorMessage(buff, 1024);


A 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.
Posted 15-Feb-13 15:53pm
Updated 16-Feb-13 15:07pm
bbirajdar 16-Feb-13 0:12am
You need to check the SQL in 'strExec' variable and check if it is valid
tomay3000 16-Feb-13 10:12am
I have executed the content of strExec with SQL Server Management Studio, and the execution was successful.
Reza Alipour Fard 16-Feb-13 0:13am
Hi, Your question has ambiguity. what mean of CDatabase, CString, ...
Please complete information of your question.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Check the return value of
masterdb.OpenEx(_T("DRIVER=SQL Server;DATABASE=master;SERVER=(local)\\SQLEXPRESS"));
database connection may not be success.

do perform the database operation only when database open is success. also its always good to put these code under try catch block.
tomay3000 16-Feb-13 10:13am
the return value of OpenEx was 0x00000001

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.180618.1 | Last Updated 16 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100