vb.net - Download URL images for products from excel sheet to insert them as images in SQL Server -
i have excel sheet contains 35,000 url products images. i'm looking way download images url's after can insert them sql server images. have no idea need help.
any ideas?
i try in standalone program - e.g. command-line utility or something. coded in c# , whatever reason, online c#-to-vb.net converters barfed , couldn't convert - hope basic idea , can in vb.net yourself.
first step: exceldatareader read excel files.
then this:
// define list of urls list<string> imageurls = new list<string>(); // open excel file , read in urls list of strings string filepath = @"c:\yoururldatafile.xlsx"; // adapt needs! // using "filestream" , "exceldatareader", read url's // list of strings using (filestream stream = file.open(filepath, filemode.open, fileaccess.read)) { using (iexceldatareader excelreader = excelreaderfactory.createopenxmlreader(stream)) { while (excelreader.read()) { string url = excelreader.getstring(0); imageurls.add(url); } excelreader.close(); } } // set necessary infrastructure storing sql server // query needs *adapted* own situation - use *your* // table , column name! string query = "insert dbo.testimages(imagedata) values(@image);"; // connection string config - again: *adapt* situation! string connectionstring = configurationmanager.connectionstrings["yourdatabase"].connectionstring; // use sqlconnection , sqlcommand in using blocks using(sqlconnection conn = new sqlconnection(connectionstring)) using (sqlcommand cmd = new sqlcommand(query, conn)) { // add parameter sql query cmd.parameters.add("@image", sqldbtype.varbinary, -1); // loop through url's - try fetch image, // , if successful, insert sql server database foreach (string url in imageurls) { try { // new "webclient", , fetch data url webclient client = new webclient(); byte[] imagedata = client.downloaddata(url); // open connection conn.open(); // set parameter data fetched url cmd.parameters["@image"].value = imagedata; // execute sql query - return value number // of rows inserted - should *1* (if successful) int inserted = cmd.executenonquery(); // close connection conn.close(); } catch (exception exc) { // log exception } } }
this should pretty need - of course, there plenty of additional things - read number of url's excel file, add more logging (also success cases etc.) - should rough skeleton of little program
Comments
Post a Comment