Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
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 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
Updated 16-Feb-13 15:07pm
v5
Comments
bbirajdar 16-Feb-13 0:12am    
You need to check the SQL in 'strExec' variable and check if it is valid
Mr. Tomay 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

Check the return value of
vc
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.
 
Share this answer
 
Comments
Mr. Tomay 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)



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