0

I have written the following code which works fine
But the problem is in the database only 3rows from csv file is reflected
and rest is not
Following is my code
Can anyone help me for the same

namespace Test1
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Uploadbutton_Click(object sender, EventArgs e)
        {

            openFileDialog1.ShowDialog();

            filenametextBox.Text = openFileDialog1.FileName;


        }//Uploadbutton_Click

        private void save_to_DBbutton_Click(object sender, EventArgs e)
        {
            try
            {

                if (openFileDialog1.FileName == "")
                {
                    MessageBox.Show("File Not Selected");
                }//if (openFileDialog1.FileName == "")
                SqlConnection connection_string = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["database"]);


                string filepath = openFileDialog1.FileName;

                StreamReader sr = new StreamReader(filepath);

                string line = sr.ReadLine();

                string[] value = line.Split(',');

                DataTable dt = new DataTable();

                DataRow row;

                foreach (string dc in value)
                {
                    dt.Columns.Add(new DataColumn(dc));
                }

                while (!sr.EndOfStream)
                {
                    value = sr.ReadLine().Split(',');

                    if (value.Length == dt.Columns.Count)
                    {
                        row = dt.NewRow();

                        row.ItemArray = value;

                        dt.Rows.Add(row);
                    }
                }
                SqlBulkCopy bc = new SqlBulkCopy(connection_string.ConnectionString, SqlBulkCopyOptions.TableLock);

                bc.DestinationTableName = System.Configuration.ConfigurationManager.AppSettings["table_name"];

                //bc.DestinationTableName = "test";
                bc.BatchSize = dt.Rows.Count;

                connection_string.Open();

                bc.WriteToServer(dt);

                bc.Close();

                connection_string.Close();

                MessageBox.Show("Data dumped to database succesfully");

                this.Close();
            }

            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }//private void save_to_DBbutton_Click

    }//public partial class Form1 : Form
}//namespace Test1
1
Contributor
1
Reply
6
Views
6 Years
Discussion Span
Last Post by Arjun_Sarankulu
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.