.net - Import excel file to sql using bulkcopy -
i have been able import excel file sql bulkcopy locally. when publish code server following error messages:
exception message: 'c:\mytest.xls' not valid path. make sure path name spelled correctly , connected server on file resides.
exception source: microsoft jet database engine
here code:
<%@ page language="vb" autoeventwireup="false" codefile="test.aspx.vb" inherits="test" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="head1" runat="server"> </head> <body> <form id="form1" runat="server"> <asp:fileupload id="txtfile" runat="server" /> <br /><br /> <asp:button id="button1" runat="server" text="button" /> </form> </body> </html> protected sub button1_click(byval sender object, byval e system.eventargs) handles button1.click dim excelconnectionstring string = "provider=microsoft.jet.oledb.4.0;data source=" & txtfile.postedfile.filename & ";extended properties=""excel 8.0;hdr=yes;""" using connection new system.data.oledb.oledbconnection(excelconnectionstring) 'list columns need excel file dim command new system.data.oledb.oledbcommand("select [name],[location] [sheet1$]", connection) connection.open() ' create dbdatareader data worksheet using dr system.data.oledb.oledbdatareader = command.executereader() ' sql server connection string dim con sqlconnection = getconnection() using bulkcopy new system.data.sqlclient.sqlbulkcopy(con) bulkcopy.destinationtablename = "tblexcel" 'define columnmappings: source(excel) --destination(db table column) bulkcopy.columnmappings.add("name", "name") bulkcopy.columnmappings.add("location", "location") bulkcopy.writetoserver(dr) end using closeconnection(con) end using end using end sub
you should first save file (saveas(path), use server.mappath map relative dir) , give full path connection string.
Comments
Post a Comment