简体   繁体   English

我如何从数据库中删除所有数据并使用 Microsoft.SqlServer.Management.Smo 插入新数据;

[英]How i can delete all data from DB and insert new using Microsoft.SqlServer.Management.Smo;

Now i use only this options现在我只使用这个选项

ScriptingOptions options = new ScriptingOptions();
options.ScriptSchema = false;
options.ScriptData = true;
options.ScriptForCreateDrop = true;
options.WithDependencies = true;

but i get output sript with only但我只得到输出 sript

INSERT [t].[Table] ([f], [f1], [f2], [f3], [f4], [f5], [f6]) VALUES (N'SomeData', N'SomeData', N'SomeData', N'SomeData', CAST(N'SomeData' AS DateTime2), CAST(N'SomeData' AS DateTime2), NULL)

but i want get DELETE ACTION before INSERT但我想在 INSERT 之前得到 DELETE ACTION

DELETE FROM [t].[Table]

INSERT [t].[Table] ([f], [f1]) VALUES (N'SomeData', N'SomeData')

How i can do it?我该怎么做? Help pls请帮助

UPD: I just use other options and save result into another virable UPD:我只是使用其他选项并将结果保存到另一个病毒

ScriptingOptions optionsForClearData = new ScriptingOptions();
            optionsForClearData.ScriptSchema = false;
            optionsForClearData.ScriptData = true;
            optionsForClearData.ScriptForCreateDrop = true;
            optionsForClearData.ScriptDrops = true;

that code give me result StringCollections with values DELETE FROM [t].[table] Than i just put it into one big string and save it like .sql rollback file using this part of code该代码给了我结果 StringCollections 值 DELETE FROM [t].[table] 比我只是将它放入一个大字符串并使用这部分代码将其保存为 .sql 回滚文件

            var tt = new StringCollection();
            var s = new List<string>();
            foreach (var tbl in Tables.Reverse())
            {
                s.AddRange(dbs.Tables[tbl].EnumScript(optionsForClearData));
            }
            foreach (var tbl in Tables)
            {
                s.AddRange(dbs.Tables[tbl].EnumScript(options));
            }
            tt.AddRange(s.ToArray());
            MemoryStream ms = new MemoryStream();
            TextWriter tw = new StreamWriter(ms);

            var text = new string[tt.Count];
            tt.CopyTo(text, 0);
            tw.Write(string.Join("\n",text));
            tw.Flush();
            ms.Position = 0;
            return new FormFile(ms, 0, ms.Length, "backup", 
            FileName+".sql");

PS: Yes i know about i missed "using" with streams (RAW CODE) PS:是的,我知道我错过了“使用”流(RAW CODE)

You shouldn't use a management library for CRUD to a db.您不应该将管理库用于 CRUD 到数据库。 You need to be sending validated queries or invoking stored procedures through a data provider您需要通过数据提供者发送经过验证的查询或调用存储过程

I would recommend Dapper which was built by Stack Overflow themselves.我会推荐由 Stack Overflow 自己构建的Dapper Very performant (I believe its only a pinch slower than ADO.NET which is pretty amazing), reduces a lot of ADO.NET boilerplate, and you have as much fine tuned control as you want as you can write your own queries非常高效(我相信它只比 ADO.NET 慢一点,这非常了不起),减少了很多 ADO.NET 样板文件,并且您可以根据需要进行微调,因为您可以编写自己的查询

Alternatively, for a low/no-code option there is also Entity Framework .或者,对于低代码/无代码选项,还有Entity Framework Not as performant, but very feature rich for sure.性能不高,但功能肯定非常丰富。

If you don't want any external libraries, as I mentioned ADO .NET is a bit more writing but is very fast如果您不想要任何外部库,正如我所提到的,ADO .NET 的编写要多一些,但速度非常快

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中使用Microsoft.SqlServer.Management.Smo加载数据 - How to load data using Microsoft.SqlServer.Management.Smo in C# 如何使用 Microsoft.SqlServer.Management.Smo 命名空间的还原类进行还原 - how to restore using restore class of Microsoft.SqlServer.Management.Smo namespace 使用Microsoft.SqlServer.Management.Smo从StoredProc获取结果列而不执行它 - Get result columns from a StoredProc without exec it, using Microsoft.SqlServer.Management.Smo Microsoft.SqlServer.Management.Smo获取SQL Server的恢复模型 - Microsoft.SqlServer.Management.Smo getting the Recovery Model of the SQL Server 在Microsoft.SqlServer.Management.Smo中找不到C#传输 - C# Transfer not found in Microsoft.SqlServer.Management.Smo 备份类未显示在Microsoft.SqlServer.Management.Smo参考中 - Backup Class Not Showing in Microsoft.SqlServer.Management.Smo Reference 什么是Microsoft.sqlserver.Management.Smo的CLSID和PROG ID - what is the CLSID and PROG ID of Microsoft.sqlserver.Management.Smo Microsoft.SqlServer.Management.Smo命名空间 - 我需要安装什么来解决它? - Microsoft.SqlServer.Management.Smo Namespace - what do I need to install to resolve it? 查找兼容的Microsoft.SqlServer.Management.Smo和ConnectionInfo引用 - Finding Microsoft.SqlServer.Management.Smo and ConnectionInfo References that are Compatible 需要与Microsoft.SqlServer.Management.Smo Transfer类的帮助连接 - Need help connection with Microsoft.SqlServer.Management.Smo Transfer class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM