1

I need to create database objects (table, index, keys, views, functions, etc.) through the c#.net code. The user specifies the name of the database object to be replicated and the new name given to the copied object.

So suppose user selects existing table TableA and the new name TableB, my code should create TableB having the same structure (columns, etc.) as TableA.

I am okay with the actual creation part. I need tips on how to get the TableA metadata (structure).

Which is the best way to achieve the same?

Thanks very much :)

0

3 Answers 3

2

Take a look into SQL Management Objects (SMO), its a library developed by microsoft for performing management tasks on SQL server databases through managed code. It includes code to script databases and the objects within them, which should satisfy a large portion of your applications requirements

Sign up to request clarification or add additional context in comments.

Comments

1

Have a look at a library called SMO (SQL Server Management Objects): http://msdn.microsoft.com/en-us/library/ms162169.aspx

Comments

1

You can get this information from the INFORMATION_SCHEMA schema.

For example:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TableA'

will give you all the columns. See here for a tutorial.

1 Comment

Except that INFORMATION_SCHEMA is a SCHEMA, not a TABLE, "COLUMNS" is the TABLE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.