java - cannot insert more than 16 columns using SQLServerBulkCopy -
i using bulk copy code msdn website, nothing changed actually. have table correct columns in db (sqlserver 2008 r2 management studio 10). when tried run code, on addcolumnmetadata line throw exception:
com.microsoft.sqlserver.jdbc.sqlserverexception: column 17 invalid. please check column mappings. @ com.microsoft.sqlserver.jdbc.sqlserverbulkcopy.validatecolumnmappings(sqlserverbulkcopy.java:1747) @ com.microsoft.sqlserver.jdbc.sqlserverbulkcopy.writetoserver(sqlserverbulkcopy.java:1514) @ com.microsoft.sqlserver.jdbc.sqlserverbulkcopy.writetoserver(sqlserverbulkcopy.java:628)
i cannot find bug. can please me? below part of code copied msdn. loop , file path things changed.
public static void main(string[] args) { string connectionstring = getconnectionstring(); sqlserverbulkcsvfilerecord filerecord = null; try { // data source file loading class implements isqlserverbulkrecord. // here using sqlserverbulkcsvfilerecord implementation import example csv file. filerecord = new sqlserverbulkcsvfilerecord("f:/test/test1.csv", true); // set metadata each column copied. for(int = 0;i < 17;i++) { filerecord.addcolumnmetadata(i+1, null, java.sql.types.varchar, 10, 0); } // open destinationconnectio adventureworks database. class.forname("com.microsoft.sqlserver.jdbc.sqlserverdriver"); try (connection destinationconnection = drivermanager.getconnection(connectionstring)) { try (statement stmt = destinationconnection.createstatement()) { // perform initial count on destination table. long countstart = 0; try (resultset rsrowcount = stmt.executequery( "select count(*) dbo.bulkcopydemodifferentcolumns1;")) { rsrowcount.next(); countstart = rsrowcount.getint(1); system.out.println("starting row count = " + countstart); } // set bulk copy object. // note column positions in source // data reader match column positions in // destination table there no need // map columns. try (sqlserverbulkcopy bulkcopy = new sqlserverbulkcopy(destinationconnection)) { bulkcopy.setdestinationtablename("dbo.bulkcopydemodifferentcolumns1"); try { // write source destination. bulkcopy.writetoserver(filerecord); } catch (exception e) { // handle errors may have occurred. e.printstacktrace(); } } // perform final count on destination // table see how many rows added. try (resultset rsrowcount = stmt.executequery( "select count(*) dbo.bulkcopydemodifferentcolumns1;")) { rsrowcount.next(); long countend = rsrowcount.getint(1); system.out.println("ending row count = " + countend); system.out.println((countend - countstart) + " rows added."); } } } } catch (exception e) { // handle errors may have occurred. e.printstacktrace(); } { if (filerecord != null) try { filerecord.close(); } catch(exception e) {} } }
everything works less 17 columns. complete code @ msdn
here table structure: create table bulkcopydemodifferentcolumns1
(
name1 varchar(10),
name2 varchar(10),
name3 varchar(10),
name4 varchar(10),
name5 varchar(10),
name6 varchar(10),
name7 varchar(10),
name8 varchar(10),
name9 varchar(10),
name10 varchar(10),
name11 varchar(10),
name12 varchar(10),
name13 varchar(10),
name14 varchar(10),
name15 varchar(10),
name16 varchar(10),
name17 varchar(10)
)
you mentioned csv file has 2 rows, first being header.
sqlserverbulkcopy object requires row terminates \r\n
, not \n
. make sure case csv.
Comments
Post a Comment