miniSql

vs2019汉化99%
zgc123@gmail.com authored at 11/28/2023 4:10:34 PM zgc123@gmail.com committed at 11/28/2023 4:10:51 PM
2.55 KiB
TruncateTableCommand.cs
#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("截断表")
        {
            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("确认截断表 '{0}' ", tableName);
            string msg = string.Format("删除表 '{0}' 的数据, 你确定?", 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, "错误");
                }
                catch (InvalidOperationException invalidExp)
                {
                    hostWindow.DisplaySimpleMessageBox(null, invalidExp.Message, "错误");
                }
                finally
                {
                    if (cmd != null)
                    {
                        cmd.Dispose();
                    }

                    hostWindow.SetPointerState(Cursors.Default);
                }
            }
        }
    }
}