Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
How to generate script of a database at run time in c#.
Posted 31 Dec '10 - 18:46
Edited 1 Jan '11 - 2:05
thatraja183K


5 solutions

Also knowing what kind of database and driver you are using would be a big help.
 
Are you talking about generating the DDL that created an object within the database?
 
For example, I use oracle so I can call the database procedure
 
dbms_metadata.get_ddl(
object_type IN VARCHAR2,
name IN VARCHAR2,
schema IN VARCHAR2 DEFAULT NULL,
version IN VARCHAR2 DEFAULT 'COMPATIBLE',
model IN VARCHAR2 DEFAULT 'ORACLE',
transform IN VARCHAR2 DEFAULT 'DDL')
RETURN CLOB;
 
by using the select statement
 
SELECT DBMS_METADATA.GET_DDL('TABLE','SCHEMA','MY_TABLE') FROM DUAL;
 
You could then use this to output the sql for all your (schema) table objects by doing
 
SELECT DBMS_METADATA.GET_DDL('TABLE',TABLE_NAME) FROM USER_TABLES;
 
This is specific for Oracle though and would be different for sql server or mysql.
  Permalink  
Build a string with DDL (Data definition language) SQL into a variable named strDDL.
Connect to your DB.
Instantiate SQL command c and set its Connection property.
Call c.ExecuteNonQuery(strDDL);
If you need to know what the exact syntax for the DDL of your specific DBMS is you'll have to consult the documentation of it.
 
Regards,
Manfred
  Permalink  
If your database is MS SQLServer then this[^] article might help.
  Permalink  
You should elaborate you question & be precise. for example Script of existing database, New database on installation etc.
  Permalink  
Hi,
 
You can try the following link for MSSQL:http://msdn.microsoft.com/en-us/library/ms178078.aspx[^]
 
I hope it will help you..
 
Regards!
Aman
  Permalink  
Comments
Marcus Kramer - 2 Jan '11 - 10:32
His issue isn't the creation of the script, it is figuring out how to run it from C# code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 586
1 Maciej Los 255
2 Slacker007 240
3 OriginalGriff 220
4 CPallini 210
0 Sergey Alexandrovich Kryukov 9,118
1 OriginalGriff 7,134
2 CPallini 3,803
3 Rohan Leuva 3,135
4 Maciej Los 2,558


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 2 Jan 2011
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid