Tuesday, February 1, 2011

CSV file to sql-server no BULK INSERT , C#, asp.net 4.0

BULK INSERT using sql-server command will not work if you have following type of data, you have to have do programming.
FName,Lname, address,state,ctr
Rajeev,kumar,"lake, view dr", CA, USA
Bulk insert will work perfectly for following type:-
FName,Lname, address,state,ctr
Rajeev,kumar,lake view dr, CA, USA
So I search and found this code in google , this is complete tested working code.
 
 
 

protected void Button1UPD_Click(object sender, EventArgs e)
    {
        if (Txtrepdat.Text == "")
        {
            Label1.Text = "Please Enter a Valid Date";
        }
        else
        {
            if (FileUpload1.PostedFile.FileName == string.Empty)
            {
                lblMsg.Visible = true;
                return;
            }
            else
            {
                //save the file
                //restrict user to upload other file extenstion
                string[] FileExt = FileUpload1.FileName.Split('.');
                string FileEx = FileExt[FileExt.Length - 1];
                if (FileEx.ToLower() == "csv")
                {
                    FileUpload1.SaveAs(Server.MapPath("CSVLoad//" + "Book1.csv"));//FileUpload1.FileName));
                }
                else
                {
                    lblMsg.Visible = true;
                    return;
                }
                // SqlDataSource1.SelectCommand = "SELECT * FROM vdcsvfl";
                gvid.Visible = true;
                Button1.Visible = true;
                CheckBox1.Visible = true;
                Label4.Visible = true;
            }
            //create object for CSVReader and pass the stream
            CSVReader reader = new CSVReader(FileUpload1.PostedFile.InputStream);
            //get the header
            string[] headers = reader.GetCSVLine();
            DataTable dt = new DataTable();
            if (dt.Columns.Count == 0)
            {
                lblMsg.Text = "Please Check your file Format";
                //    Response.End();
            }
            foreach (string strHeader in headers)
                dt.Columns.Add(strHeader);
            string[] data;
            while ((data = reader.GetCSVLine()) != null)
                dt.Rows.Add(data);
            //bind gridview
            gv.DataSource = dt;
            gv.DataBind();
            deldata();
            uploaddata();
            BindData();
            lblMsg.Text = "Done... ";
        }
    }
    void deldata()
    {
        SqlConnection myConnection = new SqlConnection(ConnectionString);
        string strSQL;
        strSQL = "delete from vdcsvfl";
        SqlCommand cmd = new SqlCommand(strSQL, myConnection);
        myConnection.Open();
        cmd.ExecuteNonQuery();
        myConnection.Close();
    }
    void uploaddata()
    {
        for (int count = 0; count < gv.Rows.Count; count++)
        {
            //you can get the bound filed values like
            string strId = gv.Rows[count].Cells[0].Text;

            SqlConnection myConnection = new SqlConnection(ConnectionString);
            string strSQL;
            strSQL = "insert into vdcsvfl (video_id,content_type,policy,video_title,video_duration,username,uploader,claim_type,claim_origin,embed_views,watch_views,share_rev, partner_sold_revenue,afv_revenue,amount_payable, has_multiple_claims, category, custom_id)values(@video_id,@content_type,@policy,@video_title,@video_duration,@username,@uploader,@claim_type,@claim_origin,@embed_views,@watch_views,@share_rev, @partner_sold_revenue,@afv_revenue,@amount_payable, @has_multiple_claims, @category, @custom_id)";//insert into stored proc
            SqlCommand cmd = new SqlCommand(strSQL, myConnection);
            cmd.Parameters.AddWithValue("@video_id", gv.Rows[count].Cells[0].Text);
            cmd.Parameters.AddWithValue("@content_type", gv.Rows[count].Cells[1].Text);
            cmd.Parameters.AddWithValue("@policy", gv.Rows[count].Cells[2].Text);
            cmd.Parameters.AddWithValue("@video_title", gv.Rows[count].Cells[3].Text);
            cmd.Parameters.AddWithValue("@video_duration", gv.Rows[count].Cells[4].Text);
            cmd.Parameters.AddWithValue("@username", gv.Rows[count].Cells[5].Text);
            cmd.Parameters.AddWithValue("@uploader", gv.Rows[count].Cells[6].Text);
            cmd.Parameters.AddWithValue("@claim_type", gv.Rows[count].Cells[7].Text);
            cmd.Parameters.AddWithValue("@claim_origin", gv.Rows[count].Cells[8].Text);
            cmd.Parameters.AddWithValue("@embed_views", gv.Rows[count].Cells[9].Text);
            cmd.Parameters.AddWithValue("@watch_views", gv.Rows[count].Cells[10].Text);
            cmd.Parameters.AddWithValue("@share_rev", gv.Rows[count].Cells[11].Text);
            cmd.Parameters.AddWithValue("@partner_sold_revenue", gv.Rows[count].Cells[12].Text);
            cmd.Parameters.AddWithValue("@afv_revenue", gv.Rows[count].Cells[13].Text);
            cmd.Parameters.AddWithValue("@amount_payable", gv.Rows[count].Cells[14].Text);
            cmd.Parameters.AddWithValue("@has_multiple_claims", gv.Rows[count].Cells[15].Text);
            cmd.Parameters.AddWithValue("@category", gv.Rows[count].Cells[16].Text);
            cmd.Parameters.AddWithValue("@custom_id", gv.Rows[count].Cells[17].Text);
            myConnection.Open();
            cmd.ExecuteNonQuery();
            myConnection.Close();
        }
    }

2 comments:

Md.Abdullah Al Hady. said...

Hello,
This is a great help. Thanks,
I am new in asp,
So will you please tell me what control i have to use in aspx page for this code.
Thanks
Ronjon

Rajeev said...

FileUpload1 & gridview