excel - OleDb set Data Source to load file window -
private sub button_import_click(sender object, e eventargs) handles button_import.click try dim myconnection oledb.oledbconnection dim ds system.data.dataset dim myadapter system.data.oledb.oledbdataadapter myconnection = new system.data.oledb.oledbconnection(string.format("provider=microsoft.ace.oledb.12.0;data source='c:\users\matil\desktop\kontaktdaten.xlsx';extended properties=excel 8.0")) myadapter = new system.data.oledb.oledbdataadapter("select * [kontaktdaten$]", myconnection) ds = new system.data.dataset myadapter.fill(ds) me.datagridview_kontakte.datasource = ds.tables(0) catch ex exception msgbox(ex.message) end try end sub
in above code, i’m importing excel data file datagridview, want make possible user choose own data source in load file window. how can achieve that?, tryed modifying code this, error "the microsoft acces database cannot open or write file".
dim path new openfiledialog() myconnection = new system.data.oledb.oledbconnection(string.format("provider=microsoft.ace.oledb.12.0;data source={0};extended properties=excel 8.0", path))
to answer comment, problem easy handle. use code filename:
dim indlg new openfiledialog indlg.defaultext = "csv" indlg.filter = "texte|*.txt|csv-files|*.csv|alle files|*.*" indlg.title = "textdatei" indlg.multiselect = false if indlg.showdialog <> dialogresult.ok messagebox.show("funktionsabbruch.", "info", messageboxbuttons.ok, messageboxicon.information) exit sub end if
in case, there line missing:
myconnection = new system.data.oledb.oledbconnection(string.format("provider=microsoft.ace.oledb.12.0;data source=" & indlg.filename & ";extended properties=excel 8.0"))
Comments
Post a Comment