I need to copy a datatable into a new SQL Server table. I don't have any schema but I know what columns the datatable has (Year,Month,Day,MeanTemp,TotalPrecip,MaxGustSpeed
)
From my researches I found that I need to use SqlBulkCopy
but couldn't make it work.
The project is a win application in VS 2010. Wonder if anyone here can help me out.
Thanks a lot
ps: it's my first post here, not sure if I've included enough details, plz let me know if any thing else needs to be mentioned. Also I am a beginner, so if you could possibly help me in an easy to understand manner
This is what I tried:
string connectionString = "server=localhost;database=CPSdata;Trusted_Connection=True";
//Open a connection with destination database;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(connection))
{
bulkcopy.DestinationTableName = "dbo.climateRawDataTable";
try
{
bulkcopy.WriteToServer(dt);
}
catch (Exception ex)
{
}
connection.Close();
}
}
The error I get is:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)