miniSql

创建
zgc123@gmail.com authored at 11/19/2023 1:40:15 AM
6136600
Tree
0 Parent(s)
Summary: 1 changed files with 69 additions and 0 deletions.
Added +69 -0
Added +69 -0
diff --git a/minisqlquery-master/src/MiniSqlQuery/PlugIns/DatabaseInspector/Commands/TruncateTableCommand.cs b/minisqlquery-master/src/MiniSqlQuery/PlugIns/DatabaseInspector/Commands/TruncateTableCommand.cs
new file mode 100644
index 0000000..373535d
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery/PlugIns/DatabaseInspector/Commands/TruncateTableCommand.cs
@@ -0,0 +1,69 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+using System;
+using System.Data;
+using System.Data.Common;
+using System.Windows.Forms;
+using MiniSqlQuery.Core;
+
+namespace MiniSqlQuery.PlugIns.DatabaseInspector.Commands
+{
+    /// <summary>The truncate table command.</summary>
+    public class TruncateTableCommand : GenerateStatementCommandBase
+    {
+        /// <summary>Initializes a new instance of the <see cref="TruncateTableCommand"/> class.</summary>
+        public TruncateTableCommand()
+            : base("Truncate Table")
+        {
+            SmallImage = ImageResource.table_delete;
+        }
+
+        /// <summary>Execute the command.</summary>
+        public override void Execute()
+        {
+            IHostWindow hostWindow = Services.HostWindow;
+            string tableName = hostWindow.DatabaseInspector.RightClickedTableName;
+
+            string caption = string.Format("Truncate '{0}' Table Confirmation", tableName);
+            string msg = string.Format("Delete all '{0}' data, are you sure?", tableName);
+            if (tableName != null && MessageBox.Show(msg, caption, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
+            {
+                DbConnection dbConnection;
+                DbCommand cmd = null;
+
+                try
+                {
+                    hostWindow.SetPointerState(Cursors.WaitCursor);
+                    dbConnection = Settings.GetOpenConnection();
+                    cmd = dbConnection.CreateCommand();
+                    cmd.CommandText = "DELETE FROM " + tableName;
+                    cmd.CommandType = CommandType.Text;
+                    cmd.ExecuteNonQuery();
+                    Services.PostMessage(SystemMessage.TableTruncated, tableName);
+                }
+                catch (DbException dbExp)
+                {
+                    hostWindow.DisplaySimpleMessageBox(null, dbExp.Message, "Error");
+                }
+                catch (InvalidOperationException invalidExp)
+                {
+                    hostWindow.DisplaySimpleMessageBox(null, invalidExp.Message, "Error");
+                }
+                finally
+                {
+                    if (cmd != null)
+                    {
+                        cmd.Dispose();
+                    }
+
+                    hostWindow.SetPointerState(Cursors.Default);
+                }
+            }
+        }
+    }
+}
\ No newline at end of file