vb.net - Export to text file from Access missing carriage return at the end of the line -
i have database in access , need export information text file problem have is not exported in correct format. example of text file been exported:
info|info1|info3|info4|info5|info6|info7|info8| <this wrong format info|info1|info3|info4|info5|info6|info7|info8 <this correct format
as can see, code inserting vertical line @ end of row instead of carriage return.
please see code , great:
dim connetionstring string dim cnn oledbconnection connetionstring = "connection string.accdb;" cnn = new oledbconnection(connetionstring) dim dtresult new datatable cnn.open() 'change query dim dataadap new oledbdataadapter("select * table", cnn) dataadap.fill(dtresult) cnn.close() 'change path desired path dim ruta string = "path want put text file\" dim archivotxt string = "filename of text file.txt" if not directory.exists(ruta) directory.createdirectory(ruta) end if dim writer new streamwriter(ruta + archivotxt) try dim sb new stringbuilder each row datarow in dtresult.rows sb = new stringbuilder each col datacolumn in dtresult.columns sb.append(row(col.columnname) & "|") next writer.writeline(sb.tostring()) next catch ex exception throw ex if not writer nothing writer.close() end try msgbox("done") end sub
instead of:
for each col datacolumn in dtresult.columns sb.append(row(col.columnname) & "|") next writer.writeline(sb.tostring())
you use: writer.writeline(string.join("|", row.itemarray))
string.join("|", row.itemarray)
concatenates tostring()
result of each value held row separating values "|".
Comments
Post a Comment