How can I insert data from Excel sheet into SQL Server using C# -


i have pretty code import url's excel , download images store them in sql server in varbinary(max) column.

this code works well, want store lot of data id, image url, image name besides binary images.

can me that?

// 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           }      } } 

table sql server looks

    create table [dbo].[inv](     [id] [bigint] not null,     [upc] [bigint] null,     [item_spec] [nvarchar](300) null,     [quantity] [int] null,     [original_cost] [money] null,     [total_o_cost] [money] null,     [retail_price_usd] [money] null,     [total_o_retail] [money] null,     [vendor_style] [nvarchar](255) null,     [colors_id] [int] null,     [size_id] [int] null,     [client_cost_usd] [money] null,     [total_client_cost] [money] null,     [div_id] [int] not null,     [depart_id] [int] not null,     [vendor_id] [int] not null,     [image] [varbinary](max) null,     [image_id] [int] null,     [location_id] [int] null,     [id_lot_number] [int] null,     [lot_number] [int] null,     [bol] [float] null,     [categories_id] [int] not null,     [return_id] [int] null,     [of_pallets] [float] null,     [of_cartons] [float] null,     [season_id] [int] null,     [hs_code] [int] null,     [sales_price] [money] null,     [brand_id] [int] not null,     [sku] [int] null,     [barcode] [nvarchar](255) null,     [sar] [money] null,     [aed] [money] null,     [bhd] [money] null,  constraint [pk_inv] primary key clustered  (     [id] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary] textimage_on [primary] 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -