vb.net - How to load csv files from the internet into an Access database? -
i have following array contains ticker symbols: public shared tickerarray() string = {"goog", "aapl", "v", "msft"}. need use loop: for each tickervalue in form1.tickerarray load csv file each ticker symbol 1 large table in microsoft access. csv files located @ "http://ichart.yahoo.com/table.csv?s=" & tickervalue. need respective ticker symbol loaded each line of table imported csv file in order make each line unique. columns in database should be: "ticker, date, open, high, low, close, volumne & adj close".
i've found information loading local csv file access can't seem figure out how load csv files internet access through vb.net.
also, need update table need insert new unique lines csv file. no duplicates.
any suggestions? thanks!
update: here code have far.
imports system.data imports system.data.oledb imports system.net imports system.io public class form1 public shared tickerarray() string = {"goog", "aapl", "v", "msft"} private sub btnupdate_click(byval sender system.object, byval e system.eventargs) handles btnupdate.click dim myconnection oledbconnection dim dbpath string = dim sconnectionstring string = "provider=microsoft.ace.oledb.12.0; data source=" & dbpath & ";persist security info=true" myconnection = new oledbconnection(sconnectionstring) myconnection.open() dim strurl string dim strbuffer string each tickervalue in form1.tickerarray 'creates request url yahoo. strurl = "http://ichart.yahoo.com/table.csv?s=" & tickervalue strbuffer = requestwebdata(strurl) 'create array. dim sreader new stringreader(strbuffer) dim list new list(of string) while sreader.peek >= 0 list.add(sreader.readline) loop dim lines string() = list.toarray sreader.close() each line in lines.skip(1) msgbox(line) dim myinsert string = texttoinsert(line, tickervalue) dim cmd new oledbcommand(myinsert, myconnection) cmd.executenonquery() next next end sub function texttoinsert(byval inputstring string, byval ticker string) dim dt string = inputstring.split(",")(0).trim dim open string = inputstring.split(",")(1).trim dim high string = inputstring.split(",")(1).trim dim low string = inputstring.split(",")(1).trim dim close string = inputstring.split(",")(1).trim dim volume string = inputstring.split(",")(1).trim dim adj_close string = inputstring.split(",")(1).trim dim sqlstr string sqlstr = "insert historical (ticker, date, open, high, low, close, volume, adj close) " & "values (" & "'" & ticker & "','" & dt & "','" & open & "','" & high & "'," & "'" & low & "','" & close & "','" & volume & "','" & adj_close & "'" & ")" return sqlstr end function private function requestwebdata(byval pstrurl string) string dim objwreq webrequest dim objwresp webresponse dim strbuffer string 'contact website objwreq = httpwebrequest.create(pstrurl) objwresp = objwreq.getresponse() 'read answer web site , store stream dim objsr streamreader objsr = new streamreader(objwresp.getresponsestream) strbuffer = objsr.readtoend objsr.close() objwresp.close() return strbuffer end function end class i error code "oledbexception unhandled. syntax error in insert statement." @ line cmd.executenonquery() please help!
you may use system.data.oledb namespace define function make insert db. (in rough way):
dim myconnection oledbconnection dim sconnectionstring string = "provider=microsoft.ace.oledb.12.0; data source=" & dbpath & ";persist security info=true" myconnection = new oledbconnection(sconnectionstring) myconnection.open() then make cycle on each line in csv
dim myinsert string= functiontexttoinsert(inputstring) dim cmd new oledbcommand(myinsert, myconnection) cmd.executenonquery() the functiontexttoinsert(byval inputstring string) function converts line csv in insert string: "max;min;vol" -> "insert mytable (column_01,column_02,column_03) values (max,min,vol);"
Comments
Post a Comment