cannot insert more than 16 columns using SQLServerBulkCopy
<p>I am using the Bulk Copy code from MSDN website, nothing changed actually. I have the table with correct columns in DB (SQLServer 2008 R2 Management Studio 10). But when I tried to run this code, on the addColumnMetadata line throw an exception:</p>

<blockquote>
  <p>com.microsoft.sqlserver.jdbc.SQLServerException: Column 17 is invalid.
  Please check your column mappings.    at
  com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.validateColumnMappings(SQLServerBulkCopy.java:1747)
    at
  com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.writeToServer(SQLServerBulkCopy.java:1514)
    at
  com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.writeToServer(SQLServerBulkCopy.java:628)</p>
</blockquote>

<p>I cannot find the bug. Can anyone please help me? Below is part of the code copied from MSDN. The for loop and file path are the only things I changed.</p>

<pre><code> public static void main(String[] args)
{
    String connectionString = GetConnectionString();
    SQLServerBulkCSVFileRecord fileRecord = null;
    try
    {            
        // Get data from the source file by loading it into a class that implements ISQLServerBulkRecord.
        // Here we are using the SQLServerBulkCSVFileRecord implementation to import the example CSV file.
        fileRecord = new SQLServerBulkCSVFileRecord("F:/test/test1.csv", true);    

        // Set the metadata for each column to be copied.
        for(int i = 0;i &lt; 17;i++)
        {
            fileRecord.addColumnMetadata(i+1, null, java.sql.Types.VARCHAR, 10, 0);
        }

        // Open a destinationConnectio to the AdventureWorks database. 
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        try (Connection destinationConnection = DriverManager.getConnection(connectionString))
        {
            try (Statement stmt = destinationConnection.createStatement())
            {
                // Perform an initial count on the destination table.
                long countStart = 0;
                try (ResultSet rsRowCount = stmt.executeQuery(
                        "SELECT COUNT(*) FROM dbo.BulkCopyDemoDifferentColumns1;"))
                {
                    rsRowCount.next();
                    countStart = rsRowCount.getInt(1);
                    System.out.println("Starting row count = " + countStart);
                }

                // Set up the bulk copy object.  
                // Note that the column positions in the source 
                // data reader match the column positions in  
                // the destination table so there is no need to 
                // map columns. 
                try (SQLServerBulkCopy bulkCopy =
                           new SQLServerBulkCopy(destinationConnection))
                {
                    bulkCopy.setDestinationTableName("dbo.BulkCopyDemoDifferentColumns1");

                    try
                    {
                        // Write from the source to the destination.
                        bulkCopy.writeToServer(fileRecord);
                    }
                    catch (Exception e)
                    {
                        // Handle any errors that may have occurred.
                        e.printStackTrace();
                    }
                }

                // Perform a final count on the destination  
                // table to see how many rows were added.
                try (ResultSet rsRowCount = stmt.executeQuery(
                        "SELECT COUNT(*) FROM dbo.BulkCopyDemoDifferentColumns1;"))
                {
                    rsRowCount.next();
                    long countEnd = rsRowCount.getInt(1);
                    System.out.println("Ending row count = " + countEnd);
                    System.out.println((countEnd - countStart) + " rows were added.");
                }
            }
        }
    }
    catch (Exception e)
    {
        // Handle any errors that may have occurred.
        e.printStackTrace();
    }
    finally
    {
        if (fileRecord != null) try { fileRecord.close(); } catch(Exception e) {}
    }
}
</code></pre>

<p>Everything works well with less than 17 columns. Complete code is at <a href="http://msdn.microsoft.com/en-us/library/mt221490(v=sql.110).aspx" rel="nofollow">msdn</a></p>

<p>Here is the table structure:
create table BulkCopyDemoDifferentColumns1<br>
(<br>
    name1 VARCHAR(10),<br>
    name2 VARCHAR(10),<br>
    name3 VARCHAR(10),<br>
    name4 VARCHAR(10),<br>
    name5 VARCHAR(10),<br>
    name6 VARCHAR(10),<br>
    name7 VARCHAR(10),<br>
    name8 VARCHAR(10),<br>
    name9 VARCHAR(10),<br>
    name10 VARCHAR(10),<br>
    name11 VARCHAR(10),<br>
    name12 VARCHAR(10),<br>
    name13 VARCHAR(10),<br>
    name14 VARCHAR(10),<br>
    name15 VARCHAR(10),<br>
    name16 VARCHAR(10),<br>
    name17 VARCHAR(10)<br>
    )  </p>