Click here to Skip to main content
12,507,674 members (52,389 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VC6 C++ SQL MFC
Hello,

I have created an MFC SDI application with ODBC support for M$ SQL Server database

I manipulate the db data with the CRecordset & CDatabase functions.

I need to create the M$ SQL Server database programmatically for the first launch of the application on deployment.

How to do so !?

Help me please.

Thank you for your understanding.
Posted 9-Feb-13 6:44am
tomay30001.3K
Updated 9-Feb-13 6:45am
v2
Comments
   
What did you try?
—SA
tomay3000 9-Feb-13 13:26pm
   
I have no idea.
I just use the CRecordset functions directly
MoveNext();
MoveLast();
Add();
...
PIEBALDconsult 9-Feb-13 13:01pm
   
I wouldn't do it that way; I'd have a separate utility to create the database. I also wouldn't use ODBC.
tomay3000 9-Feb-13 13:31pm
   
How do you do it !?
Do you deploy an empty database with the application !?
PIEBALDconsult 9-Feb-13 14:47pm
   
Yes. With any static data already present.
tomay3000 9-Feb-13 15:34pm
   
Thanks
CHill60 9-Feb-13 16:53pm
   
Absolutely! Do you want to post this as the solution?
PIEBALDconsult 9-Feb-13 17:02pm
   
No.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

I have found a solution, & this is a test code which uses the master db to create the test db

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();

NB: you have to verify if the test db has been created before first (I am working around on this).
  Permalink  
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The easiest way for you is to create a SQL file for table creation and embed that in your application then execute the following command : http://msdn.microsoft.com/en-us/library/ms162773.aspx[^]
  Permalink  

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 | Mobile
Web02 | 2.8.160927.1 | Last Updated 17 Feb 2013
Copyright © CodeProject, 1999-2016
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