我正在使用 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);
}
以下内容经过测试:
NuGet 包:
所需的程序集引用(对于.NET4.*):
注意: .NET Core 框架系列似乎尚未完全实现 SMO。请参阅此问题查看示例。
我创建了一个简单的控制台应用程序并调用了以下函数。
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 = "";
//Script out Tables
foreach( Table myTable in database.Tables )
{
foreach( string s in scripter.EnumScript( new Urn[] { myTable.Urn } ) )
scrs += s + "\n\n"; ;
}
//Script out Views
foreach( View myView in database.Views )
{
//Skip system views
//There is a scripter.Options.AllowSystemObjects = false; setting that does the same but it is glacially slow
if( myView.IsSystemObject == true ) continue;
foreach( string s in scripter.EnumScript( new Urn[] { myView.Urn } ) )
scrs += s + "\n\n"; ;
}
return ( scrs );
}
函数的调用方式如下:
// Connection string for local SQL Server default instance
ScriptDatabase( "Server=.;Database=PlayGround;Trusted_Connection=True;", "PlayGround" );
注意:如果出现以下错误,请参阅此问题
“已成功与服务器建立连接,但在登录过程中发生错误。(提供商:SSL 提供商,错误:0 - 证书链由不受信任的机构颁发。)”
输出:
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 参考资料:
2019 年 12 月 17 日更新: 检查最新的 .NET 版本;添加了所需的参考文献;清理示例代码;添加了示例连接字符串
2023 年 8 月 15 日更新: 检查 .NET 7.0 版本;添加了所需的NuGet包;添加导出查看示例