今天看到CSDN上一個網友問的問題,寫下了這段代碼:
我的问题是:SQL SERVER中用binary类型存储的日期,我想用delphi开发软件通过adoquery控件在查询出的记录,在dbgrid表格中显示出具体的发生日期和时间。例如在SQL SERVER中binary的期是0x01C969D24ED59A80,而在dbgrid表格中显示的格式为2008年11月11日14时22分22秒456毫秒。
測試表結構:
-
SQL code
-
create table test(A int ,b image )
-
Delphi(Pascal) code
-
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, DB, ADODB; type TForm1 = class(TForm) ADOConnection1: TADOConnection; ADOQuery1: TADOQuery; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end ; var Form1: TForm1; implementation { $R *.dfm } procedure TForm1.Button1Click(Sender: TObject); var a : TUnicodeEncoding; vBin : TBytes; begin a : = TUnicodeEncoding.Create; vBin : = a.GetBytes(DateTimeToStr(now())); adoquery1.Close; adoquery1.SQL.Text : = ' select * from test ' ; ADOQuery1.Open; adoQuery1.Append; ADOQuery1.FieldByName( ' a ' ).Value : = 1 ; TBlobField(ADOQuery1.FieldByName( ' b ' )).Value : = vBin; ADOQuery1.post; end ; procedure TForm1.Button2Click(Sender: TObject); var a : TUnicodeEncoding; vBin : TBytes; begin a : = TUnicodeEncoding.Create; adoquery1.Close; adoquery1.SQL.Text : = ' select * from test ' ; ADOQuery1.Open; vBin : = TBlobField(ADOQuery1.FieldByName( ' b ' )).Value ; showMessage(a.GetString(vBin)); end ; end .