p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/asp-net-3-5-basics-351/)
-   -   Copying csv file to database(sql server) using c# (http://p2p.wrox.com/asp-net-3-5-basics/99115-copying-csv-file-database-sql-server-using-c.html)

mas_Far August 2nd, 2016 05:37 AM

Copying csv file to database(sql server) using c#
i want to copy .csv files to database without the repetition of data.so far i have copied the .csv while without checking the validation of repetition . i need to aslo verify that same data columns are not present

This is my Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace dataReader
public partial class _default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)


protected void Upload(object sender, EventArgs e)
//Upload and save the file
string csvPath = Server.MapPath("~/Farheen Files") + Path.GetFileName(FileUpload1.PostedFile.FileName);

DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });

string csvData = File.ReadAllText(csvPath);
foreach (string row in csvData.Split('\n'))
if (!string.IsNullOrEmpty(row))
int i = 0;
foreach (string cell in row.Split(','))
dt.Rows[dt.Rows.Count - 1][i] = cell;

string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
//Set the database table name
sqlBulkCopy.DestinationTableName = "dbo.Customers";

All times are GMT -4. The time now is 04:49 PM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.