f



Save JPEG images in SQL as binary BLOB data from MATLAB without using DATABASE toolbox

clc;
clear all;
close all;

%Connection credentials
catalog = <Database Name>;
table =  <table name>;
connection.user = '<user>';
connection.pass = '<pass>';

try
    
    %-- opens connection to the database --%
    conn = actxserver('ADODB.Connection');
    openStr = ('Provider=sqloledb;Data Source=<Server Name/IP>; Initial Catalog=%s ;User Id=%s; Password=%s;');
    openStr = sprintf(openStr,catalog,connection.user,connection.pass);
    conn.Open(openStr);
catch ex
    rethrow(ex)
    
end

%Provide the source folder name where images are stored
SourceFolder = <Source Folder>;

%Getting all the files in Source folder
Files=dir(SourceFolder);

%%
%Read all the images from folder, loop through it and save it in SQL
cd(SourceFolder);

for k=3:length(Files)
    
    %Getting the complete file name and time stamp from relative file names
    FileName = [SourceFolder,'\',Files(k).name;];
        
    %building the SQL query 
    query0 = 'insert into %s.dbo.%s VALUES(%s)';

    values =  sprintf ('''%s'',(select * FROM openrowset(BULK ''%s'', SINGLE_BLOB) AS BLOB)',...
                                                    FileName);
    
    %final query to be executed 
    query = sprintf(query0,catalog,table,values);
    %try printing this query to matlab output, copy and paste, execute in SQL query window in SQL Management studio. If successful
    %the query is right. Building the query in right format might be time consuming.	 

    
    try
        conn.Execute(query);
    catch ex
        rethrow(ex);
    end
end

conn.Close();
delete(conn);
0
jintgeor (2)
3/19/2012 6:23:14 PM
comp.soft-sys.matlab 211264 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

0 Replies
1508 Views

Similar Articles

[PageSpeed] 56

Reply: