3

I try to generate a script which is contains datas in INSERT script forms, I try to use a reference (see below) to do it

using Microsoft.SqlServer.Management.Smo;

with this code draft

Server serv = new Server(db);
Database simba = serv.Databases[dbname];
string script = "";

ScriptingOptions so = new ScriptingOptions()
{
    ScriptData = true,
    ScriptSchema = false,
    ScriptDrops = false
};

foreach (Table tb in simba.Tables)
{
    if (tables.Contains(tb.Name))
    {
        var sc = tb.Script(so);
        foreach (var s in sc)
            script += s;

    }
    using (StreamWriter writer = new StreamWriter(file))
    {
        foreach (string tab in tables)
        writer.WriteLine(script);
    }
}

but this code get an error on

var sc = tb.Script(so);

which is

Microsoft.SqlServer.Management.Smo.FailedOperationException

thanks for all reply

Tab Alleman
  • 31,483
  • 7
  • 36
  • 52
  • 3
    What is the rest of the exception detail? – Crowcoder Mar 12 '18 at 14:03
  • Show example of the script you're running – BugFinder Mar 12 '18 at 14:06
  • @bugfinder I think they are trying to generate a string with scripts, not run them. Have you tried explicitly typing the string? – Jacob H Mar 12 '18 at 14:07
  • @Crowcoder Microsoft.SqlServer.Management.Smo.FailedOperationException: Cette méthode ne prend pas en charge le script de données. à Microsoft.SqlServer.Management.Smo.SqlSmoObject.ScriptImpl(ScriptingOptions so) à Microsoft.SqlServer.Management.Smo.TableViewTableTypeBase.Script(ScriptingOptions scriptingOptions) à generateScriptSQL.Program.Main(String[] args) dans c:\users\cad\Source\Repos\generateScriptSQL\generateScriptSQL\Program.cs:ligne 84 – christopher achard Mar 12 '18 at 14:12
  • @JacobH do you mean without generating? i can use the publisher wizard directly on mssql but i try to automatisate my task – christopher achard Mar 12 '18 at 14:15
  • No I meant strictly typing instead of implicit `var`. Not sure if that's the issue. Where is `tables` coming from? – Jacob H Mar 12 '18 at 14:17
  • what's wrong in my question? why i get a bad point?? – christopher achard Mar 12 '18 at 14:17
  • Possible duplicate of [ScriptingOptions sql smo does not support scripting data](https://stackoverflow.com/questions/37003017/scriptingoptions-sql-smo-does-not-support-scripting-data) – Crowcoder Mar 12 '18 at 14:19
  • Indent the code - it is hard to read. { } do not match. You can pull `ScriptingOptions so` out of the loop. But I don't know why it is failing. – paparazzo Mar 12 '18 at 14:19
  • same error, tables is a list of what i want to generate – christopher achard Mar 12 '18 at 14:20

2 Answers2

4

I have this code and it is working fine try to use it

var report   = string.Empty;
var fileName = Server.MapPath(Constants.BACKUP_FILE_NAME);

var server      = new Server(new ServerConnection(new SqlConnection(Constants.BACKUP_CONNECTION_STRING)));
var options     = new ScriptingOptions();
var databases   = server.Databases[Constants.BACKUP_DATABASE_NAME];                    

options.FileName                = fileName;
options.EnforceScriptingOptions = true;
options.WithDependencies        = true;
options.IncludeHeaders          = true;
options.ScriptDrops             = false;
options.AppendToFile            = true;
options.ScriptSchema            = true;
options.ScriptData              = true;
options.Indexes                 = true;

report = "<h4>Table Scripts</h4>";
foreach (var table in Constants.BACKUP_TABLES)
{
    databases.Tables[table, Constants.BACKUP_SCHEMA_NAME].EnumScript(options);                        
    report += "Script Generated: " + table + "<br>";
}

The "Constants" is my class to hold the constant values like file name, db etc and I am generating script for limited tables so for that reason not doing "simba.Tables" like you have done in your code; you can surely do that if you want every table scripts to be generated. So this code generates script and store it to specified file.

Hope it helps

Zaki Mohammed
  • 969
  • 14
  • 24
3

Thanks @Zaki Mohammed,

Your code helped me a lot,

I just modify a bit for my case and it works perfectly,

            Server serv = new Server(db);
            Database simba = serv.Databases[dbname];
            Scripter scripter = new Scripter(serv);
            scripter.Options.FileName = "InsertIntoScript.sql";
            scripter.Options.EnforceScriptingOptions = true;
            scripter.Options.WithDependencies = false;
            scripter.Options.IncludeHeaders = true;
            scripter.Options.ScriptDrops = false;
            scripter.Options.AppendToFile = true;
            scripter.Options.ScriptSchema = false;
            scripter.Options.ScriptData = true;
            scripter.Options.Indexes = false;

            string script = "";
            foreach (Table tb in simba.Tables)
            {
                if (tables.Contains(tb.Name))
                {
                    IEnumerable<string> sc = scripter.EnumScript(new Urn[] { tb.Urn });
                    foreach (var s in sc)
                        script += s;
                }
            }
  • NOTE: the script += stuff can be very slow with large data. Using the following got me from over three minutes to about 7 seconds. StringBuilder script = new StringBuilder(); ... script.Append(s); – Derek Apr 28 '21 at 17:28