0
votes

I have the following code to import a csv file to my local sql database :

  Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("SQLConnStr").ConnectionString)
        cn.Open()
        Using copy As New SqlBulkCopy(cn)
            copy.ColumnMappings.Add(0, 0)
            copy.ColumnMappings.Add(1, 1)
            copy.ColumnMappings.Add(2, 2)
            copy.ColumnMappings.Add(3, 3)
            copy.ColumnMappings.Add(4, 4)
            copy.ColumnMappings.Add(5, 5)
            copy.ColumnMappings.Add(6, 6)
            copy.ColumnMappings.Add(7, 7)
            copy.ColumnMappings.Add(8, 8)

            copy.DestinationTableName = "Table"
            copy.WriteToServer(dt)
        End Using
    End Using

At the line : copy.WriteToServer(dt), I get the following error : Cannot access destination table 'Table'. I know dt was created and filled, by using a break point and clicking on the magnifying glass when I hover over dt in the code.

I am using a local sql database with the following connection string :

Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\SkedDataBasis.mdf;Integrated Security=True

If have tried to make Integrated security = False, but to no avail.

Any help would be greatly appreciated. Regards

1

1 Answers

0
votes

Do you have a table named "Table" in your database? I'll wager not, which would explain why the error message says that it couldn't be accessed. What is the name of the table that you want to insert the data into? That is what you need to assign to that DestinationTableName property.