1. 程式人生 > >Delphi 將JPG圖片上傳到SqlServer資料庫裡

Delphi 將JPG圖片上傳到SqlServer資料庫裡

elphi 將JPG圖片上傳到SqlServer資料庫裡 


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Jpeg, StdCtrls, ADODB, DB;

type
TForm1 = class(TForm)
Button1: TButton;
ADOQuery1: TADOQuery;
procedure UploadJPGToSqlServer(UserName, path, ConnStr: String);
procedure Button1Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
//www.3464.com
implementation

{$R *.dfm}
procedure TForm1.UploadJPGToSqlServer(UserName, path, ConnStr: String);
var
tb: TADOQuery;
Begin

tb := TADOQuery.Create(self);
tb.ConnectionString := ConnStr;

// 刪除資料
tb.SQL.Clear;
tb.SQL.Text := 'Delete From JPEG Where UserName='''+UserName+'''';

tb.ExecSQL;

// 插入資料
tb.SQL.Clear;
tb.SQL.Text := 'Select * from JPEG Where UserName='''+UserName+'''';
tb.Open;
if not(tb.State in [dsEdit, dsInsert]) then tb.Insert;
(tb.FieldByName('JPEG') as TBlobField).LoadFromFile(path);
tb.FieldByName('UserName').Text := UserName;
tb.Post;
tb.Close;
tb.Free;
MessageBox(handle, '上傳成功' ,'系統提示' , MB_OK);


End;

procedure TForm1.Button1Click(Sender: TObject);
var
StrConn, UserName, Path: String;
begin
StrConn := 'Provider=SQLOLEDB.1;Password=#這裡是密碼#;Persist Security Info=True;User ID=#這裡是Sql登陸名#;Initial Catalog=#這裡是資料庫名#;Data Source=#這裡是資料庫的地址#';
UserName := '龔韜';
Path := 'C:\Inetpub\wwwroot\yizhi\Images\bgtitle.jpg';
UploadJPGToSqlServer(UserName, Path, StrConn);
end;

end.