Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Updated 9-Feb-13 6:45am
v2
Comments
Sergey Alexandrovich Kryukov 9-Feb-13 12:49pm    
What did you try?
—SA
Mr. Tomay 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.
Mr. Tomay 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.

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

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


NB: you have to verify if the test db has been created before first (I am working around on this).
 
Share this answer
 
v4
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[^]
 
Share this answer
 

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