Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC6 C++ SQL MFC
	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 15-Feb-13 16:53pm
tomay30001.2K
Edited 16-Feb-13 16:07pm
v5
Comments
aspnet_regiis -i at 16-Feb-13 0:12am
   
You need to check the SQL in 'strExec' variable and check if it is valid
tomay3000 at 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 at 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
good
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.
  Permalink  
Comments
tomay3000 at 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
0 Afzaal Ahmad Zeeshan 256
1 OriginalGriff 251
2 BillWoodruff 240
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 16 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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