// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Linq; using System.Text; using Microsoft.SqlServer.Management.Common; using Microsoft.SqlServer.Management.Smo; namespace E { public class Program { public static void Main(string[] args) { Server srv = new Server(@"ARNEPC3\SQLEXPRESS"); srv.ConnectionContext.LoginSecure = true; srv.ConnectionContext.Connect(); Scripter scr = new Scripter(srv); scr.Options.ScriptSchema = true; scr.Options.ScriptData = true; // works StringBuilder sb = new StringBuilder(); foreach (string s in scr.EnumScript(srv.Databases["Test"].Tables.Cast().ToArray())) { sb.Append(s); sb.Append(Environment.NewLine); } string res = sb.ToString(); srv.ConnectionContext.Disconnect(); Console.WriteLine(res); } } }