Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i try to import csv data into sql server using window application. but i get this error while import the data.Any suggestion to fix this error?

What I have tried:

private void btnImport_Click(object sender, EventArgs e)
{
DataTable dataTable = new DataTable();
string dbconnection = @"Data Source=; Initial Catalog=Biiuty; Initial Catalog=Hbi; User Id=ida; Password=***"";
using (SqlConnection sqlConnection = new SqlConnection(dbconnection))
{


SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlConnection);
sqlBulkCopy.DestinationTableName = "SIS";
sqlBulkCopy.ColumnMappings.Add("DateTime", "DateTime");
sqlBulkCopy.ColumnMappings.Add("Milliseconds", "Milliseconds");
sqlBulkCopy.ColumnMappings.Add("MachineAutoStartStop", "MachineAutoStartStop");
sqlBulkCopy.ColumnMappings.Add("Pressure", "Pressure");

sqlConnection.Open();
sqlBulkCopy.WriteToServer(dataTable);
sqlConnection.Close();


}
Posted
Updated 12-Dec-22 16:47pm

1 solution

Simple really. The DataTable you created has no columns in it. It's completely empty and this is what you are trying to bulk copy to the SQL table.

No columns means there are no column names for the ColumnMappings to match up to.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900