GVKun编程网logo

将长字符串分配给 int 停止 SSMS 处理并防止灾难性的“裸”F5 狂奔(将字符串常量存放到字符串变量时,包含)

18

对于想了解将长字符串分配给int停止SSMS处理并防止灾难性的“裸”F5狂奔的读者,本文将提供新的信息,我们将详细介绍将字符串常量存放到字符串变量时,包含,并且为您提供关于c字符串分配差异?、delp

对于想了解将长字符串分配给 int 停止 SSMS 处理并防止灾难性的“裸”F5 狂奔的读者,本文将提供新的信息,我们将详细介绍将字符串常量存放到字符串变量时,包含,并且为您提供关于c字符串分配差异?、delphi – 将自动完成字符串分配给的访问冲突、delphi – 正确的灾难性错误处理、java – 处理灾难性异常的有价值信息。

本文目录一览:

将长字符串分配给 int 停止 SSMS 处理并防止灾难性的“裸”F5 狂奔(将字符串常量存放到字符串变量时,包含)

将长字符串分配给 int 停止 SSMS 处理并防止灾难性的“裸”F5 狂奔(将字符串常量存放到字符串变量时,包含)

今天在 SSMS 中,我放错了指针并单击了“执行”按钮而不是“数据库”下拉菜单(它们在屏幕上相邻)。幸运的是,没有造成任何损害,但让我感到害怕的是,我可能已经在当前查询窗口中执行了所有操作,因为没有突出显示任何内容。我想在任何新查询窗口的顶部放置一个简单的命令,这将停止 F5 类型的执行。这似乎有效:

UPDATE atable SET intfield = ''freakout prevention'' WHERE tablekey = 123UPDATE atable SET intfield = 55 WHERE tablekey = 123

其中 intfield 是定义为 int 的列。运行两条线导致

将 varchar 值 ‘freakout Prevention’ 转换为数据类型 int 时转换失败。

此外,intfield 的值未设置为 55。

这是一个相当可靠的方法吗(我在这里不需要 100.00% - 足以捕捉我不小心执行“裸” F5 的罕见时间)“前缀”查询窗口以防止在没有突出显示时疯狂执行的方法并给出了执行命令?

答案1

小编典典

如果您没有批次(例如,GO),那么您可以将 RETURN 作为第一行。

如果您需要阻止所有批处理运行,您可以在脚本开头放置一个延迟。或者你可以添加一些消息....

我编辑了这个答案以添加一些额外的代码来检查未结交易。这与最初的问题无关,但我发现这对一些开发人员来说是一个更大的问题。

RAISERROR(''You ran me by accident.  I will be wait for an hour for you to kill me.'', 10, 1) WITH NOWAITWHILE (1=1) BEGIN    WAITFOR DELAY ''1:00:00''    RAISERROR(''I''''m still waiting.  Please kill me.  Here goes another hour...'', 10, 1) WITH NOWAITENDGORAISERROR(''OMG!  Get the backups ready for a restore in production!  Also, update the resume.'', 16, 1) WITH NOWAITGOBEGIN TRANGO-- [Updated] Extra check - open transactionWHILE ( @@TRANCOUNT > 0 ) BEGIN    RAISERROR(''Execution is almost complete; however, a transaction is open.  Please COMMIT or ROLLBACK after you kill me.  Waiting 1 minute...'', 10, 1) WITH NOWAIT    WAITFOR DELAY ''0:01:00''ENDRAISERROR(''Execution is complete.'', 10, 1) WITH NOWAIT

c字符串分配差异?

c字符串分配差异?

我有一个函数,它将char *作为唯一的参数.然后我对它执行一些strtok操作.有时它有效,有时甚至有效.它的工作取决于字符串的构造方式.例如,这是两个案例.

int main()
{
   char glob[] = "/abc/def/ghi";
   char *glob2 = "/abc/def/ghi";

   func(glob);  //this one works
   func(glob2); //this one doesnt work

   return 0;
}

这两种分配方法有什么区别,为什么strtok会在第二种分配方法上爆炸?

解决方法

strtok()基本上修改了输入字符串.

char *glob2 = "/abc/def/ghi";

在上面的例子中,glob2指向只读数据,因此它失败了,而’char glob [] =“/ abc / def / ghi”;’数据不是只读的,它在char数组中可用.因此它允许修改.

delphi – 将自动完成字符串分配给的访问冲突

delphi – 将自动完成字符串分配给的访问冲突

我正在使用自动完成功能修改编辑控件:
Auto append/complete from text file to an edit box delphi

我想从DB加载自动完成字符串.我在自动完成控件后代上声明了新属性:

FACDataSource : TDataSource;
FACFieldName : string;

我这称之为加载自动完成字符串:

procedure TAutoCompleteEdit.ReadSuggestions;
begin
  FAutoCompleteSourceList.Clear;

  if (not Assigned(FACDataSource)) or (not Assigned(FACDataSource.DataSet))       or (not ACEnabled) then
      exit;

    with FACDataSource.DataSet do
    begin
       if Active and (RecordCount > 0) and (FACFieldName <> '') then
       begin
         First;
         while not EOF do
         begin
                       FAutoCompleteSourceList.Add(FACDataSource.DataSet.FieldByName(FACFieldName).Asstring);
    Next;
  end;
  if FAutoCompleteSourceList.Count > 0 then
    ACStrings := FAutoCompleteSourceList;
end;

结束;
结束;

但是,在将FAutoCompleteSourceList分配给ACStrings时,我获得了AccessViolation. ACStrings的setter是:

procedure TAutoCompleteEdit.SetACStrings(const Value: TStringList);
begin
    if Value <> FACList.FStrings then
      FACList.FStrings.Assign(Value);
end;

我在行中获得了AccessViolation:FACList.FStrings.Assign(Value); (阅读地址XXXYYY).定义了值,而不是那时的垃圾(例如,我可以在调试器中查看字符串列表). ‘FStrings’是一个空的字符串列表.

当控件放在窗体上时,它工作正常.但是如果我将它放在用户输入DBGridEH单元格时显示的自定义inplace编辑器中,则不会.

inplace编辑器是这样的:

unit UInplaceAutoCompleteEditor;

    interface
    uses UDBAutoComplete,UMyInplaceEditor,classes,windows,Controls,Buttons,DB;

    type TInplaceAutoCompleteEditor = class(TMyInplaceEditor)
      private
       FEditor : TAutoCompleteEdit;
    FButton : TSpeedButton;
    FShowButton : boolean;
    procedure SetShowButton(value : boolean);
    public
        constructor Create(AOwner : TComponent); override;
        procedure SetFocus; override;
        destructor Destroy; override;
     protected
    procedure EditorKeyDown(Sender : TObject; var Key : Word; Shift : TShiftState);
    function GetACDataSource : TDataSource;
    procedure SetACDataSource(value : TDataSource);
    function GetACFieldName : string;
    procedure SetACFieldName(value : string);
    procedure SetACEnabled(value : boolean);
    function GetACEnabled : boolean;
  published
    property Editor : TAutoCompleteEdit read FEditor;
    property ACDataSource : TDataSource read GetACDataSource write SetACDataSource;
    property ACFieldName : string read GetACFieldName write SetACFieldName;
    property ACEnabled : boolean read GetACEnabled write SetACEnabled;
    property Button : TSpeedButton read FButton;
    property ShowButton : boolean read FShowButton write SetShowButton;
end;

  procedure Register;

implementation

  procedure Register;
  begin
    RegisterComponents('nikolaev',[ TInplaceAutoCompleteEditor ]);
  end;

{ TInplaceAutoCompleteEditor }



constructor TInplaceAutoCompleteEditor.Create(AOwner: TComponent);
begin
  inherited;
  FEditor := TAutoCompleteEdit.Create(self);
  FEditor.Parent := self;
  FEditor.Align := alClient;
  FEditor.Visible := true;
  FEditor.WantTabs := true;
  FEditor.OnKeyDown := EditorKeyDown;

  FButton := TSpeedButton.Create(self);
  FButton.Parent := self;
  FButton.Align := alRight;

  self.FOwnHeight := -1;
  self.FOwnWidth := -1;

  SetShowButton(false);
end;

destructor TInplaceAutoCompleteEditor.Destroy;
begin
  Feditor.Destroy;
  FButton.Destroy;
  inherited;
end;

procedure TInplaceAutoCompleteEditor.EditorKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if Key in [ VK_Return,VK_Tab ] then
  begin
    self.Value := FEditor.Text;
    Key := 0;
    ConfirmValue;
  end;

  if Key = VK_Escape then
  begin
    Key := 0;
    CancelValue;
  end;

  inherited;
end;



function TInplaceAutoCompleteEditor.GetACDataSource: TDataSource;
begin
  Result := FEditor.ACDataSource;
end;

function TInplaceAutoCompleteEditor.GetACEnabled: boolean;
begin
  Result := FEditor.ACEnabled;
end;

function TInplaceAutoCompleteEditor.GetACFieldName: string;
begin
  Result := FEditor.ACFieldName
end;

procedure TInplaceAutoCompleteEditor.SetACDataSource(value: TDataSource);
begin
  FEditor.ACDataSource := value;
end;

procedure TInplaceAutoCompleteEditor.SetACEnabled(value: boolean);
begin
  FEditor.ACEnabled := value;
end;

procedure TInplaceAutoCompleteEditor.SetACFieldName(value: string);
begin
  FEditor.acfieldname := value;
end;

procedure TInplaceAutoCompleteEditor.SetFocus;
begin
  inherited;
  FEditor.SetFocus;
end;

procedure TInplaceAutoCompleteEditor.SetShowButton(value: boolean);
begin
  if value <> FShowButton then
  begin
    FShowButton := value;
    FButton.Visible := value;
  end;
end;

end.

这个inplace编辑器继承自这样的抽象类:

unit UMyInplaceEditor;

interface
uses Windows,types,dbGridEh,ExtCtrls,Controls;

type TMyInplaceEditor = class (TWinControl)

  private
    FOnValueConfirmed : TNotifyEvent;
    FOnCanceled : TNotifyEvent;
    FWantTabs : boolean;
    procedure AdjustPosition;
  protected
    FOwnHeight,FOwnWidth : integer;
    FValue : Variant;
    function GetIsEditing : boolean;
    procedure SetIsEditing(value : boolean); virtual;
    procedure ConfirmValue;
    procedure CancelValue;
    procedure SetValue(val : Variant); virtual;
  public

    property OnValueConfirmed : TNotifyEvent read FOnValueConfirmed write FOnValueConfirmed;
    property OnCanceled : TNotifyEvent read FOnCanceled write FOnCanceled;
    property Value : Variant read FValue write SetValue;


    property IsEditing : boolean read GetIsEditing write SetIsEditing;
    procedure SetPosition(parentControl : TWinControl; rect : TRect); virtual;

    function ColumnEditable(column : TColumnEH) : boolean; virtual;

    constructor Create(AOwner : TComponent); override;
    property WantTabs : boolean read FWantTabs write FWantTabs;
end;
  procedure Register;

implementation

  procedure Register;
  begin
    RegisterComponents('nikolaev',[TMyInplaceEditor]);
  end;

constructor TMyInplaceEditor.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);
  self.AutoSize := false;
  self.Visible := false;
  self.FOwnHeight := -1;
  self.FOwnWidth := -1;
end;

procedure TMyInplaceEditor.AdjustPosition;
var xOffset,yOffset : integer;
begin
  xoffset := self.Left + self.Width - self.Parent.Width;
  if xOffset > 0 then
    self.Left := self.Left - xOffset;

  yOffset := self.Top + self.Height - self.Parent.height;
  if yOffset > 0 then
    self.Top := self.Top - yOffset;

end;

function TMyInplaceEditor.GetIsEditing : boolean;
begin
  Result := self.Visible;
end;

procedure TMyInplaceEditor.SetIsEditing(value: Boolean);
begin
  self.Visible := value;
  self.BringToFront;
  {if Visible then
    self.SetFocus;}
end;

procedure TMyInplaceEditor.SetPosition(parentControl : TWinControl; rect: TRect);
begin
  self.Parent := parentControl;
  self.Top := rect.Top;//parentControl.Top;
  self.Left := rect.Left;//parentControl.left;
  if self.FOwnWidth = -1 then
    self.Width := rect.Right - rect.Left
  else
    self.Width := self.FOwnWidth;

    if self.FOwnHeight = -1 then
    self.Height := rect.Bottom - rect.Top
  else
    self.Height := self.FOwnHeight;
  AdjustPosition;
end;

function TMyInplaceEditor.ColumnEditable(column : TColumnEH) : boolean;
begin
  Result := true;
end;

procedure TMyInplaceEditor.ConfirmValue;
begin
  if Assigned(FOnValueConfirmed) then
    FOnValueConfirmed(self);
end;

procedure TMyInplaceEditor.CancelValue;
begin
  if Assigned(FOnCanceled) then
    FOnCanceled(self);
end;

procedure TMyInplaceEditor.SetValue(val : Variant);
begin
  FValue := val;
end;

end.

InplaceEditor用于DBGridEH的后代.在某些情况下,我重写ShowEditor和HideEditor来显示/隐藏我的编辑器.

同样,自动完成控件仅在嵌入inplaceeditor控件时抛出异常.

是什么导致访问违规?

解决方法

问题是您使用的代码错误处理接口引用计数.以下是相关摘录:

type
  TEnumString = class(TInterfacedobject,IEnumString)
  ....

请注意,此类派生自TInterfacedobject,因此它使用引用计数来管理其生命周期.

然后代码继续这样:

type
  TAutoCompleteEdit = class(TEdit)
  private
    FACList: TEnumString;
  ....

所以我们将持有对象而不是接口的引用.这看起来很可疑.

然后我们这样做:

constructor TAutoCompleteEdit.Create(AOwner: TComponent);
begin
  inherited;
  FACList := TEnumString.Create;
  ....
end;

destructor TAutoCompleteEdit.Destroy;
begin
  FACList := nil;
  inherited;
end;

这里没有任何东西可以让对象保持活力.在代码中的其他位置,我们引用了IEnumString接口.但是一旦该引用被释放,该对象就认为没有引用.所以它被删除了.然后,稍后,代码引用FACList,它现在指向已被销毁的对象.

解决此问题的一种简单方法是确保TAutoCompleteEdit控件始终保持对接口的引用:

type
  TAutoCompleteEdit = class(TEdit)
  private
    FACList: TEnumString;
    FEnumString: IEnumString;
....
constructor TAutoCompleteEdit.Create(AOwner: TComponent);
begin
  inherited;
  FACList := TEnumString.Create;
  FEnumString := FACList;
  ....
end;

然后,通过此更改,您可以删除TAutoCompleteEdit的析构函数,因为FEnumString后面的对象将被引用计数机制销毁.

另一种解决方法是更改​​TEnumString以禁用自动引用计数.这看起来像这样:

type
  TEnumString = class(TObject,IInterface,IEnumString)
  private
    function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
    ....
  end;

function TEnumString.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
  if GetInterface(IID,Obj) then
    Result := 0
  else
    Result := E_NOINTERFACE;
end;

function TEnumString._AddRef: Integer; 
begin
  Result := -1;
end;

function TEnumString._Release: Integer; 
begin
  Result := -1;
end;

然后你需要TAutoCompleteEdit析构函数看起来像这样:

destructor TAutoCompleteEdit.Destroy;
begin
  FACList.Free;
  inherited;
end;

最后一个选择是避免持有一个TEnumString而只保留一个IEnumString引用.让引用计数管理生命周期与第一个解决方案一样.但是,您需要实现另一个允许TAutoCompleteEdit获取TStrings对象的接口.

delphi – 正确的灾难性错误处理

delphi – 正确的灾难性错误处理

有一些我一直在努力的东西,我真的没有用Delphi程序解决,并想知道是否有人可以指导我.正如主题所说,你如何做正确的灾难性错误处理?例如:

// is file necessary for the program present?
if not FileExists(FilePath1) then
   begin
     raise Exception.Create(FilePath1 + ' does not exist and is required for this program to function.');
   // I obvIoUsly need to do something here to make the program QUIT and not have
   // any more code run.

     Application.Terminate;
     Abort;
   end;

我也可以在那里使用异常单元并抛出异常,但程序继续像以前一样.我过去曾经使用过停止呼叫,但它似乎没有进行任何清理等等,所以我最终制定了一个大程序,通过近距离和免费调用我所做的一切就是为了确保(即使那时我我不确定任何幕后的东西.

那么处理这类事情的正确方法是什么?

编辑:为了澄清,我想知道如何让程序做它需要做的清理,然后立即退出而不做任何其他代码.

解决方法

要执行异常终止,请调用Halt()传递退出代码.

if CatastropicErrorDetected then
begin
  ... show error message
  Halt(1);
end;

在Windows上,这会导致调用TerminateProcess并在那时停止执行.

您注意到没有执行清理,通常这就是您想要的.由于您在应用程序启动时执行此检查,因此无需清理任何内容.

java – 处理灾难性异常

java – 处理灾难性异常

我在C#入门书中读到,如果你不知道如何处理它,你不应该发现异常.在使用 Java编程时考虑到这些建议,我有时会发现我不知道如何处理异常,但我不得不抓住它或“渗透它”以避免编译错误.我宁愿不要在调用树的整个过程中使用throws子句混乱方法,所以我经常使用“转换”异常到RuntimeException,如下所示.将throws子句添加到许多方法中,对于没有真正“处理”(正确处理)的异常,这似乎是冗长和分散注意力的.是以下不好的风格,如果是这样,有什么更好的方法来处理这个?
try {
  thread.join();
}
catch (InterruptedException e) {
      Console.printwriter.format("%s\n",e.printstacktrace());
  throw new RuntimeException();
}

编辑:除了杂乱之外,渗透异常还有另一个问题:在代码修改之后,你可能最终会得到一些不必要的抛出子句.我知道清除它们的唯一方法是通过反复试验:删除它们并查看编译器是否抱怨.显然,如果您想保持代码清洁,这很烦人.

解决方法

在已检查和未检查的异常之间的Java划分是 somewhat controversial.

如果您控制接口,则向签名添加throws子句通常是最好的方法.

如果您处于无法处理异常的情况,但由于检查了异常签名而不允许它冒泡,那么将异常包装到可以重新抛出的异常(通常是RuntimeException)是常见的做法.

在许多情况下,您可能希望使用另一个已检查的异常,例如IOException或sqlException.但这并不总是一种选择.

但在您的示例中,将原始异常包含为“原因”:

throw new RuntimeException(e);

这也可以消除对日志记录的需要(因为这也可以推迟到可以处理异常的人,并且所有信息仍然存在).

今天的关于将长字符串分配给 int 停止 SSMS 处理并防止灾难性的“裸”F5 狂奔将字符串常量存放到字符串变量时,包含的分享已经结束,谢谢您的关注,如果想了解更多关于c字符串分配差异?、delphi – 将自动完成字符串分配给的访问冲突、delphi – 正确的灾难性错误处理、java – 处理灾难性异常的相关知识,请在本站进行查询。

本文标签: