4

How do I generate SQL database script using C# code or SQL code?

enter image description here

We can use the Generate Scripts option from ssms but how do I generate SQL database script using code?

1 Answer 1

9

You should use SQL Server SMO (Server Management Object) that includes exactly the features you're looking for. You can script single database, or specific objects. Actually, that's how SSMS does it.

Blog post: http://weblogs.asp.net/shahar/generating-sql-backup-script-for-tables-amp-data-from-any-net-application-using-smo

MSDN sample for scripting database: https://msdn.microsoft.com/en-us/library/ms205825.aspx

https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.scripter.aspx

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

Comments

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.