Hi,
I am using the SQLServerBulkCopy class to copy data from a csv file into table. However I cannot insert into a table with more than 16 columns. When the number of columns is less than 17, say 16, it is working perfectly. Is anyone having the similar issue?
I am using JAVA and Microsoft SQL Server 2008 R2 Management Studio 10. The csv file only have two lines, the 1st line is the header and the 2nd line is the data. All the 17 columns are VARCHAR(10) and they have the same value "1".
import java.sql.*;
import com.microsoft.sqlserver.jdbc.SQLServerBulkCSVFileRecord;
import com.microsoft.sqlserver.jdbc.SQLServerBulkCopy;
public class Program
{
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 < 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) {}
}
}
// To avoid storing the sourceConnection String in your code,
// you can retrieve it from a configuration file.
private static String GetConnectionString()
{
}
}
Here is the exception msg:
com.microsoft.sqlserver.jdbc.SQLServerException: Column 17 is invalid. Please check your column mappings.
at com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.validateColumnMappings(SQLServerBulkCopy.java:1690)
at com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.writeToServer(SQLServerBulkCopy.java:1510)
at com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.writeToServer(SQLServerBulkCopy.java:616)
And this is the table schema:
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)
)