ADO操作数据库

1、连接Excel

procedure TForm1.ConnectToExcel(EFileName: string); 
var
strConn: widestring;
str:TStrings;
i:Integer;
begin
str:=TStringList.Create;

//表形式,第一行为列名
strConn := 'Provider=Microsoft.Jet.OLEDB.4.0;'
+ 'Data Source=' + EFileName + ';' +
'Extended Properties=IMEX=1;Excel 8.0;HDR=false;Persist Security Info=False';

//标题头为第一行的值,标题为F1,F2....
{ strConn := 'Provider=Microsoft.Jet.OLEDB.4.0;'
+ 'Data Source=' + EFileName + ';' +
'Extended Properties=IMEX=1;Excel 8.0;Mode=Read;HDR=NO;Persist Security Info=False';
}

AdoConnection1.Connected := False;
AdoConnection1.ConnectionString := strConn;
try
AdoConnection1.Connected:=true;
AdoConnection1.GetTableNames(str, True);
except
ShowMessage('不能连接到给定的Excel文件确认给定的 ');
raise;
end;

with AdoQuery1 do
begin
Close;
Sql.Clear;
Sql.Text:='select * from [机民井调查表$]'; //表为为Sheet名称
Open;
end;
end;

2、ADOQuery查看字段类型

     //得取某字段类型

     case ADOQuery1.FieldByName(FieldName).DataType of

          ftString, ftMemo, ftFmtMemo,

          ftFixedChar, ftWideString:

          begin

              pType:='字符型';

          end;

          ftSmallint, ftInteger, ftWord,

            ftAutoInc, ftLargeInt, ftArray:

          begin

              pType:='整型';

          end;

          ftFloat, ftCurrency, ftBCD:

          begin

              pType :='浮点型';

          end;

          ftDate, ftDateTime:

          begin

              pType:='期日型';

          end;

      end;





      //所有字段类型

        TFieldType = (ftUnknown, ftString, ftSmallint, ftInteger, ftWord,

      ftBoolean, ftFloat, ftCurrency, ftBCD, ftDate, ftTime, ftDateTime,

      ftBytes, ftVarBytes, ftAutoInc, ftBlob, ftMemo, ftGraphic, ftFmtMemo,

      ftParadoxOle, ftDBaseOle, ftTypedBinary, ftCursor, ftFixedChar, ftWideString,

      ftLargeint, ftADT, ftArray, ftReference, ftDataSet, ftOraBlob, ftOraClob,

      ftVariant, ftInterface, ftIDispatch, ftGuid, ftTimeStamp, ftFMTBcd);

3、UDL文件的使用

ADOConnection1.ConnectionString:= 'FILE NAME=' + ExtractFilePath(Application.ExeName); + 'Data\data.udl';

4、插入、读取图片文件到数据库

    4.1、读取图片到Image控件

//uses jpeg

var

    MS: TMemoryStream;

    JpegImage:TJpegImage;

begin

    try

        MS := TMemoryStream.Create;

        TBlobField(ADOQuery1.FieldByName('img')).SaveToStream(MS);

        MS.Position := 0;

        JpegImage := TJpegImage.Create;

        JpegImage.LoadFromStream(MS);

        Image1.Picture.Graphic := JPegImage;

    finally

        JpegImage.Free;

        MS.Free;

    end;

end;

    4.2、插入图片

//jpeg

TBlobField(AODQuery1.FieldByName('img')).Assign(Image1.Picture.Graphic);

 

 

 

你可能感兴趣的:(数据库)