?
Solved

Creating SQL .mdf File through program in MFC

Posted on 2008-02-04
4
Medium Priority
?
1,342 Views
Last Modified: 2013-12-14
Hi,
I am working on a VC++ project in which i want to create a SQL .MDF Database at the run time and want to create table in that database file to store the data.Bt i unable to create the connection with SQL so that  ican create a mdf file.
Is anybody know how to create .MDF file Programmatically in MFC. I have SQL SERVER 2000 installed on my Machine.
tx
0
Comment
Question by:davinder101
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:alb66
ID: 20813870
You can create a .sql script and execute it from MFC application using ShellExecute().
0
 

Author Comment

by:davinder101
ID: 20838397
I don't have any idea of .sql script.
Is there any other way to do so.
Means can we use SQLDMO.dll to create a sql database .mdf file. Whene i automate this dll in my project i get sqldmo.cpp and its header file in my project in which there are some class like _application,SQLServers etc... is there any way to use that classes.
if any one have any idea to use sqldmo classes pls help me regarding this.
0
 
LVL 19

Accepted Solution

by:
alb66 earned 1500 total points
ID: 20914613
I send you an example of a script that create an SQL .MDF database, all the table and relationship, key and index.
To run it you can use the osql.exe utility that comes with SQL server (and of course you can run it from MFC code):
http://msdn2.microsoft.com/en-us/library/aa213087(SQL.80).aspx


CREATE DATABASE [StoricoMoSy]  ON (NAME = N'StoricoMoSy_Data', FILENAME = N'C:\Programmi\Microsoft SQL Server\MSSQL\data\StoricoMoSy_Data.MDF' , SIZE = 115, FILEGROWTH = 10%) LOG ON (NAME = N'StoricoMoSy_Log', FILENAME = N'C:\Programmi\Microsoft SQL Server\MSSQL\data\StoricoMoSy_Log.LDF' , SIZE = 214, FILEGROWTH = 10%)
 COLLATE Latin1_General_CI_AS
GO
 
exec sp_dboption N'StoricoMoSy', N'autoclose', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'bulkcopy', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'trunc. log', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'torn page detection', N'true'
GO
 
exec sp_dboption N'StoricoMoSy', N'read only', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'dbo use', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'single', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'autoshrink', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'ANSI null default', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'recursive triggers', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'ANSI nulls', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'concat null yields null', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'cursor close on commit', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'default to local cursor', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'quoted identifier', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'ANSI warnings', N'false'
GO
 
exec sp_dboption N'StoricoMoSy', N'auto create statistics', N'true'
GO
 
exec sp_dboption N'StoricoMoSy', N'auto update statistics', N'true'
GO
 
if( ( (@@microsoftversion / power(2, 24) = 8) and (@@microsoftversion & 0xffff >= 724) ) or ( (@@microsoftversion / power(2, 24) = 7) and (@@microsoftversion & 0xffff >= 1082) ) )
	exec sp_dboption N'StoricoMoSy', N'db chaining', N'false'
GO
 
use [StoricoMoSy]
GO
 
CREATE TABLE [dbo].[Articoli] (
	[IDart] [int] IDENTITY (1, 1) NOT NULL ,
	[IDmac] [int] NULL ,
	[Art] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
	[Tag] [int] NULL ,
	[Data] [datetime] NULL ,
	[IDturno] [int] NULL ,
	[Pezzi] [int] NULL ,
	[Scarti] [int] NULL ,
	[Teorici] [int] NULL ,
	[TempoCiclo] [int] NULL ,
	[Conta1] [int] NULL ,
	[Conta2] [int] NULL ,
	[Conta3] [int] NULL ,
	[Conta4] [int] NULL ,
	[Conta5] [int] NULL ,
	[Conta6] [int] NULL ,
	[Conta7] [int] NULL ,
	[Conta8] [int] NULL ,
	[TempoStop] [float] NULL ,
	[TempoScollegata] [float] NULL ,
	[TempoFuoriProduzione] [float] NULL ,
	[TempoCicloNormale] [float] NULL ,
	[TempoCicloParticolare] [float] NULL ,
	[Efficienza] [real] NULL ,
	[IDout] [int] NULL ,
	[Status] [int] NULL ,
	[Scarti1] [int] NULL ,
	[Scarti2] [int] NULL ,
	[Scarti3] [int] NULL ,
	[Scarti4] [int] NULL ,
	[Scarti5] [int] NULL ,
	[Scarti6] [int] NULL ,
	[Scarti7] [int] NULL ,
	[Scarti8] [int] NULL ,
	[Scarti9] [int] NULL ,
	[IDoperator] [int] NULL 
) ON [PRIMARY]
GO
 
CREATE TABLE [dbo].[Gruppi] (
	[IDgrp] [int] IDENTITY (1, 1) NOT NULL ,
	[Nome] [nvarchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
	[Status] [int] NULL 
) ON [PRIMARY]
GO
 
CREATE TABLE [dbo].[Macchine] (
	[IDmac] [int] IDENTITY (1, 1) NOT NULL ,
	[Cod] [int] NULL ,
	[Aghi] [int] NULL ,
	[Diametro] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
	[Com] [ntext] COLLATE Latin1_General_CI_AS NULL ,
	[IDtipo] [int] NULL ,
	[IDgrp1] [int] NULL ,
	[IDgrp2] [int] NULL ,
	[IDgrp3] [int] NULL ,
	[Status] [int] NULL 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
 
CREATE TABLE [dbo].[MacchineFuoriProduzione] (
	[IDout] [int] IDENTITY (1, 1) NOT NULL ,
	[IDmac] [int] NULL ,
	[DataOut] [datetime] NULL ,
	[DataIn] [datetime] NULL ,
	[Motivo] [nvarchar] (256) COLLATE Latin1_General_CI_AS NULL 
) ON [PRIMARY]
GO
 
CREATE TABLE [dbo].[Messaggi] (
	[IDmsg] [int] IDENTITY (1, 1) NOT NULL ,
	[IDtipo] [int] NULL ,
	[Stato] [int] NULL ,
	[Messaggio] [nvarchar] (255) COLLATE Latin1_General_CI_AS NULL ,
	[Status] [int] NULL 
) ON [PRIMARY]
GO
 
CREATE TABLE [dbo].[Operatori] (
	[IDoperator] [int] IDENTITY (1, 1) NOT NULL ,
	[Nome] [nvarchar] (50) COLLATE Latin1_General_CI_AS NOT NULL 
) ON [PRIMARY]
GO
 
CREATE TABLE [dbo].[Qualita] (
	[IDquality] [int] IDENTITY (1, 1) NOT NULL ,
	[IDart] [int] NOT NULL ,
	[Stato] [int] NOT NULL ,
	[Occorrenze] [int] NOT NULL ,
	[Scarti1] [int] NOT NULL ,
	[Scarti2] [int] NOT NULL ,
	[Scarti3] [int] NOT NULL ,
	[Scarti4] [int] NOT NULL ,
	[Scarti5] [int] NOT NULL ,
	[Scarti6] [int] NOT NULL ,
	[Scarti7] [int] NOT NULL ,
	[Scarti8] [int] NOT NULL ,
	[Scarti9] [int] NOT NULL ,
	[Status] [int] NOT NULL ,
	[IDtipo] [int] NOT NULL 
) ON [PRIMARY]
GO
 
CREATE TABLE [dbo].[Sacche] (
	[IDsacca] [int] IDENTITY (1, 1) NOT NULL ,
	[IDmac] [int] NULL ,
	[Tag] [int] NULL ,
	[Pezzi] [int] NULL ,
	[DataIni] [datetime] NULL ,
	[DataFin] [datetime] NULL ,
	[Status1] [nvarchar] (1) COLLATE Latin1_General_CI_AS NULL ,
	[Status2] [nvarchar] (1) COLLATE Latin1_General_CI_AS NULL ,
	[Status3] [nvarchar] (1) COLLATE Latin1_General_CI_AS NULL ,
	[Art] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
	[IDturno] [int] NULL 
) ON [PRIMARY]
GO
 
CREATE TABLE [dbo].[Sistema] (
	[ID] [int] IDENTITY (1, 1) NOT NULL ,
	[Lock] [int] NULL ,
	[Compatta] [int] NULL ,
	[Versione] [int] NULL 
) ON [PRIMARY]
GO
 
CREATE TABLE [dbo].[SommaCrono] (
	[IDcro] [int] IDENTITY (1, 1) NOT NULL ,
	[IDart] [int] NULL ,
	[Stato] [int] NULL ,
	[Durata] [float] NULL ,
	[Occorrenze] [int] NULL ,
	[Status] [int] NULL ,
	[IDtipo] [int] NULL 
) ON [PRIMARY]
GO
 
CREATE TABLE [dbo].[TipiMacchina] (
	[IDtipo] [int] IDENTITY (1, 1) NOT NULL ,
	[Marca] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
	[Gruppo] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
	[Tipo] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
	[St] [nvarchar] (50) COLLATE Latin1_General_CI_AS NULL ,
	[Status] [int] NULL 
) ON [PRIMARY]
GO
 
CREATE TABLE [dbo].[Turni] (
	[IDturno] [int] IDENTITY (1, 1) NOT NULL ,
	[Turno] [int] NULL ,
	[InizioTurno] [datetime] NULL ,
	[FineTurno] [datetime] NULL ,
	[Tipo] [nvarchar] (1) COLLATE Latin1_General_CI_AS NULL ,
	[Status] [int] NULL 
) ON [PRIMARY]
GO
 
CREATE TABLE [dbo].[Utenti] (
	[Utente] [nvarchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
	[Chiave] [nvarchar] (50) COLLATE Latin1_General_CI_AS NOT NULL 
) ON [PRIMARY]
GO
 
ALTER TABLE [dbo].[Articoli] WITH NOCHECK ADD 
	CONSTRAINT [PK_Articoli] PRIMARY KEY  CLUSTERED 
	(
		[IDart]
	)  ON [PRIMARY] 
GO
 
ALTER TABLE [dbo].[Gruppi] WITH NOCHECK ADD 
	CONSTRAINT [PK_Gruppi] PRIMARY KEY  CLUSTERED 
	(
		[IDgrp]
	)  ON [PRIMARY] 
GO
 
ALTER TABLE [dbo].[Macchine] WITH NOCHECK ADD 
	CONSTRAINT [PK_Macchine] PRIMARY KEY  CLUSTERED 
	(
		[IDmac]
	)  ON [PRIMARY] 
GO
 
ALTER TABLE [dbo].[MacchineFuoriProduzione] WITH NOCHECK ADD 
	CONSTRAINT [PK_MacchineFuoriProduzione] PRIMARY KEY  CLUSTERED 
	(
		[IDout]
	)  ON [PRIMARY] 
GO
 
ALTER TABLE [dbo].[Messaggi] WITH NOCHECK ADD 
	CONSTRAINT [PK_Messaggi] PRIMARY KEY  CLUSTERED 
	(
		[IDmsg]
	)  ON [PRIMARY] 
GO
 
ALTER TABLE [dbo].[Operatori] WITH NOCHECK ADD 
	CONSTRAINT [PK_Operatori] PRIMARY KEY  CLUSTERED 
	(
		[IDoperator]
	)  ON [PRIMARY] 
GO
 
ALTER TABLE [dbo].[Qualita] WITH NOCHECK ADD 
	CONSTRAINT [PK_Qualita] PRIMARY KEY  CLUSTERED 
	(
		[IDquality]
	)  ON [PRIMARY] 
GO
 
ALTER TABLE [dbo].[Sacche] WITH NOCHECK ADD 
	CONSTRAINT [PK_Sacche] PRIMARY KEY  CLUSTERED 
	(
		[IDsacca]
	)  ON [PRIMARY] 
GO
 
ALTER TABLE [dbo].[Sistema] WITH NOCHECK ADD 
	CONSTRAINT [PK_Sistema] PRIMARY KEY  CLUSTERED 
	(
		[ID]
	)  ON [PRIMARY] 
GO
 
ALTER TABLE [dbo].[SommaCrono] WITH NOCHECK ADD 
	CONSTRAINT [PK_SommaCrono] PRIMARY KEY  CLUSTERED 
	(
		[IDcro]
	)  ON [PRIMARY] 
GO
 
ALTER TABLE [dbo].[TipiMacchina] WITH NOCHECK ADD 
	CONSTRAINT [PK_TipiMacchina] PRIMARY KEY  CLUSTERED 
	(
		[IDtipo]
	)  ON [PRIMARY] 
GO
 
ALTER TABLE [dbo].[Turni] WITH NOCHECK ADD 
	CONSTRAINT [PK_Turni] PRIMARY KEY  CLUSTERED 
	(
		[IDturno]
	)  ON [PRIMARY] 
GO
 
 CREATE  INDEX [IX_Mac] ON [dbo].[Articoli]([IDmac]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Turno] ON [dbo].[Articoli]([IDturno]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Out] ON [dbo].[Articoli]([IDout]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Articoli] ON [dbo].[Articoli]([IDart]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Operator] ON [dbo].[Articoli]([IDoperator]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Gruppi] ON [dbo].[Gruppi]([IDgrp]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Grp1] ON [dbo].[Macchine]([IDgrp1]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Grp2] ON [dbo].[Macchine]([IDgrp2]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Grp3] ON [dbo].[Macchine]([IDgrp3]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Macchine] ON [dbo].[Macchine]([IDmac]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Mac] ON [dbo].[MacchineFuoriProduzione]([IDmac]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_MacchineFuoriProduzione] ON [dbo].[MacchineFuoriProduzione]([IDout]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Tipo] ON [dbo].[Messaggi]([IDtipo]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Messaggi] ON [dbo].[Messaggi]([IDmsg]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Operatori] ON [dbo].[Operatori]([IDoperator]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Art] ON [dbo].[Qualita]([IDart]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Tipo] ON [dbo].[Qualita]([IDtipo]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Qualita] ON [dbo].[Qualita]([IDquality]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Mac] ON [dbo].[Sacche]([IDmac]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Sacche] ON [dbo].[Sacche]([IDsacca]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Art] ON [dbo].[SommaCrono]([IDart]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Tipo] ON [dbo].[SommaCrono]([IDtipo]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_SommaCrono] ON [dbo].[SommaCrono]([IDcro]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_TipiMacchina] ON [dbo].[TipiMacchina]([IDtipo]) ON [PRIMARY]
GO
 
 CREATE  INDEX [IX_Turni] ON [dbo].[Turni]([IDturno]) ON [PRIMARY]
GO
 
ALTER TABLE [dbo].[Articoli] ADD 
	CONSTRAINT [FK_Articoli_Macchine] FOREIGN KEY 
	(
		[IDmac]
	) REFERENCES [dbo].[Macchine] (
		[IDmac]
	) ON DELETE CASCADE ,
	CONSTRAINT [FK_Articoli_Turni] FOREIGN KEY 
	(
		[IDturno]
	) REFERENCES [dbo].[Turni] (
		[IDturno]
	) ON DELETE CASCADE 
GO
 
ALTER TABLE [dbo].[Macchine] ADD 
	CONSTRAINT [FK_Macchine_Gruppi1] FOREIGN KEY 
	(
		[IDgrp1]
	) REFERENCES [dbo].[Gruppi] (
		[IDgrp]
	),
	CONSTRAINT [FK_Macchine_Gruppi2] FOREIGN KEY 
	(
		[IDgrp2]
	) REFERENCES [dbo].[Gruppi] (
		[IDgrp]
	),
	CONSTRAINT [FK_Macchine_Gruppi3] FOREIGN KEY 
	(
		[IDgrp3]
	) REFERENCES [dbo].[Gruppi] (
		[IDgrp]
	),
	CONSTRAINT [FK_Macchine_TipiMacchina] FOREIGN KEY 
	(
		[IDtipo]
	) REFERENCES [dbo].[TipiMacchina] (
		[IDtipo]
	) ON DELETE CASCADE 
GO
 
alter table [dbo].[Macchine] nocheck constraint [FK_Macchine_Gruppi1]
GO
 
alter table [dbo].[Macchine] nocheck constraint [FK_Macchine_Gruppi2]
GO
 
alter table [dbo].[Macchine] nocheck constraint [FK_Macchine_Gruppi3]
GO
 
ALTER TABLE [dbo].[MacchineFuoriProduzione] ADD 
	CONSTRAINT [FK_MacchineFuoriProduzione_Macchine] FOREIGN KEY 
	(
		[IDmac]
	) REFERENCES [dbo].[Macchine] (
		[IDmac]
	) ON DELETE CASCADE 
GO
 
ALTER TABLE [dbo].[Messaggi] ADD 
	CONSTRAINT [FK_Messaggi_TipiMacchina] FOREIGN KEY 
	(
		[IDtipo]
	) REFERENCES [dbo].[TipiMacchina] (
		[IDtipo]
	) ON DELETE CASCADE 
GO
 
ALTER TABLE [dbo].[Qualita] ADD 
	CONSTRAINT [FK_Qualita_Articoli] FOREIGN KEY 
	(
		[IDart]
	) REFERENCES [dbo].[Articoli] (
		[IDart]
	) ON DELETE CASCADE 
GO
 
ALTER TABLE [dbo].[Sacche] ADD 
	CONSTRAINT [FK_Sacche_Macchine] FOREIGN KEY 
	(
		[IDmac]
	) REFERENCES [dbo].[Macchine] (
		[IDmac]
	) ON DELETE CASCADE 
GO
 
ALTER TABLE [dbo].[SommaCrono] ADD 
	CONSTRAINT [FK_SommaCrono_Articoli] FOREIGN KEY 
	(
		[IDart]
	) REFERENCES [dbo].[Articoli] (
		[IDart]
	) ON DELETE CASCADE 
GO

Open in new window

0
 

Author Comment

by:davinder101
ID: 21376370
There is no solution which helps me to solve my problem. So please close the question.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question