ScriptingOptions sql smo不支持脚本数据

时间:2016-05-03 11:34:51

标签: c# sql sql-server sql-server-2008

我使用c#代码生成sql数据库脚本。

以下代码适用于create table,但当我尝试使用scriptOptions.ScriptData = true;时,它会抛出以下异常。

  

未处理的类型异常   ' Microsoft.SqlServer.Management.Smo.FailedOperationException'发生了   在Microsoft.SqlServer.Smo.dll

中      

其他信息:此方法不支持脚本数据。

 public static string ScriptDatabase(string dbConnectionString, string databaseName)
        {

            SqlConnection conn = new SqlConnection(dbConnectionString);
            ServerConnection serverConn = new ServerConnection(conn);
            var server = new Server(serverConn);
            var database = server.Databases[databaseName];

            var scripter = new Scripter(server);
           // I tried this code also       
           // scripter.Options.ScriptData = true;
            ScriptingOptions scriptOptions = new ScriptingOptions();
            scriptOptions.ScriptDrops = false;
            scriptOptions.ScriptData = true;
            scriptOptions.ScriptSchema = true;


            scriptOptions.IncludeIfNotExists = true;
            string scrs = "";
            string tbScr = "";
            foreach (Table myTable in database.Tables)
            {
                /* Generating IF EXISTS and DROP command for tables */
                StringCollection tableScripts = myTable.Script(scriptOptions);
                foreach (string script in tableScripts)
                    scrs += script + "\n\n";

                /* Generating CREATE TABLE command */
                tableScripts = myTable.Script();
                foreach (string script in tableScripts)
                    tbScr += script + "\n\n";
            }
 return (scrs + "\n\n" + tbScr);
}

1 个答案:

答案 0 :(得分:14)

以下适用于我的机器(Win 7.0,.NET 4.0,VS 2010) 我用OK按钮创建了一个简单的Win表单应用程序,并在下面调用了函数 注意:我最初添加if(myTable.IsSystemObject == true)继续;确保系统视图没有编写脚本;我还没有测试过这是否真的需要:

public static string ScriptDatabase( string dbConnectionString, string databaseName )
    {

        SqlConnection conn = new SqlConnection( dbConnectionString );
        ServerConnection serverConn = new ServerConnection( conn );
        var server = new Server( serverConn );
        var database = server.Databases[ databaseName ];

        var scripter = new Scripter( server );
        scripter.Options.IncludeIfNotExists = true;
        scripter.Options.ScriptSchema = true;
        scripter.Options.ScriptData = true;


        string scrs = "";
        string tbScr = ""; //no longer needed
        foreach( Table myTable in database.Tables )
        {
            if( myTable.IsSystemObject == true ) continue;

            foreach( string s in scripter.EnumScript( new Urn[] { myTable.Urn } ) )
                scrs += s + "\n\n"; ;
        }
        return ( scrs + "\n\n" + tbScr );
    }

输出:

SET ANSI_NULLS ON

SET QUOTED_IDENTIFIER ON

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tBlah]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tBlah](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [tID] [varchar](20) COLLATE Latin1_General_CI_AS NULL,
    [Value] [varchar](20) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]
END

SET IDENTITY_INSERT [dbo].[tBlah] ON 


INSERT [dbo].[tBlah] ([ID], [tID], [Value]) VALUES (1, N'2', N'1234')

INSERT [dbo].[tBlah] ([ID], [tID], [Value]) VALUES (2, N'2', N'345.6')

MSDN参考: