column Id in table is identity ,how to update column id to sql tables importing from CSV
Loading
column Id in table is identity ,how to update column id to sql tables importing from CSV
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Mar 13, 2023, 6:34 AM EDT
You can use SqlBulkCopy object to copy data from the CSV file to the database table.
string csvPath = @"C:\YourCsvFile.csv"; string tableName = "YourTableName"; string connectionString = "Data Source=YourServer;Initial Catalog=YourDatabase;Integrated Security=True"; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); // Create a DataTable to hold the CSV data. DataTable csvData = new DataTable(); using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + tableName + "]", @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + csvPath + ";Extended Properties='Text;HDR=YES;FMT=Delimited'")) { adapter.Fill(csvData); } // Use a SqlBulkCopy object to copy the data from the DataTable to the database table. using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection)) { bulkCopy.DestinationTableName = tableName; bulkCopy.WriteToServer(csvData); } connection.Close();Jignesh KumarRank #91 · 22.9K reputationPosted Mar 13, 2023, 3:12 AM EDT
If you are using import wizard then you need to enable insert identity,
https://www.c-sharpcorner.com/article/how-to-import-data-from-excel-data-into-sql-table/
Meghana MRank #8,429 · 222 reputationPosted Mar 13, 2023, 3:53 AM EDT
not using import wizard ,using C# console application
Jignesh KumarRank #91 · 22.9K reputationPosted Mar 13, 2023, 7:42 AM EDT
Hello MEghana,
You can refer this one which will help you out,
https://www.c-sharpcorner.com/UploadFile/0c1bb2/inserting-csv-file-records-into-sql-server-database-using-as/