Generate script Insert in .net
I have problem when trying generate script insert with specific condition.
So far I am already trying this step.
Add references Microsoft.SqlServer.ConnectionInfo and Microsoft.SqlServer.Smo
Add reference in code Microsoft.SqlServer.Management.Smo
Add this to s开发者_Go百科cript.
var srv = new Server(@"localhost\SQLEXPRESS"); var db = srv.Databases["Northwind"]; foreach (Microsoft.SqlServer.Management.Smo.Table tab in db.Tables) { foreach (string s in tab.Script()) { File.AppendAllText("sql_insert_conditioanl.sql", s); } }
Script "sql_insert_conditioanl.sql" contain only to create database and create table that exist in database "Northwind".
So I do research about this problem. Trying change method "Script" without parameter to parameter "ScriptingOptions" ref link. I still confuse to using parameter Scripting Options.
Then also trying using protected method. But since it protected, I can't test method (like: Method "ScriptAlter").
Regard
You need to generate scripts for all database objects - for example indices, stored procedures, views etc. See this article (and its related articles) for getting started.
精彩评论