Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello can any one help me i am develop Asp.Net application where i want to insert data in data base through excel sheet.For this i am using the following code that is work nice.but its insert data directly in to the data base.i want to add SQL query that specifies those data that i am insert..but i don't know where and how i use SQL query that insert data in database
ASP.NET
<body>
    <form id="form1" runat="server">
    <div>
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button Text="Upload" OnClick = "Upload" runat="server" />
    <asp:Label Text="" ID="label1" runat="server" />
    
  </div>
    </form>
</body>


Code for button

C#
protected void Upload(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string path = string.Concat((Server.MapPath("~/temp/" + FileUpload1.FileName)));
                FileUpload1.PostedFile.SaveAs(path);
                OleDbConnection oleDbcon = new OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0;Data Source ="+path+";Extended Properties=Excel 12.0;");
                OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", oleDbcon);
                OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
                oleDbcon.Open();
                DbDataReader dr = cmd.ExecuteReader();
                string con_str =@"Data Source =.\SQLEXPRESS; Initial Catalog = CR_SYS; Integrated Security = True";
                SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);

//this code insert data in database directly but i want sql query fro this to add data in StudentBulk table
                bulkInsert.DestinationTableName = "StudentBulk";


                bulkInsert.WriteToServer(dr);
                oleDbcon.Close();
                Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))), File.Delete);
                label1.ForeColor = Color.Blue;
                label1.Text = "Succesful";
            }
            else
            {
                label1.ForeColor = Color.Red;
                label1.Text = "slelct the file";

            }
        }


What I have tried:

I want to add a sql query for insertion
Posted
Updated 31-Jul-16 19:11pm

In that case:-
First take Excel sheet value in a datatable like

C#
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", OleDbcon);
                OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
                ds = new DataSet();
                objAdapter1.Fill(ds);
                Dt = ds.Tables[0];

After that using for loop you can achieve your goal like as

C#
private void InsertData()
    {
        for (int i = 0; i < Dt.Rows.Count; i++)
        {
            DataRow row = Dt.Rows[i];
            int columnCount = Dt.Columns.Count;
            string[] columns = new string[columnCount];
            for (int j = 0; j < columnCount; j++)
            {
                columns[j] = row[j].ToString();
            }
            conn.Open();
            string sql = "INSERT INTO TestTable(A,B,C,D,E)";
            sql += "VALUES('" + columns[0] + "','" + columns[1] + "','" + columns[2] + "',Convert(varchar(10),'" + columns[3] + "',103),'" + columns[4] + "')";
            SqlCommand cmd = new SqlCommand(sql, conn);
 
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }


Hope this will be your answer.
 
Share this answer
 
v2
SqlBulkCopy just do through loop method, each time a new row data is inserted in sql through SQL Insert query nothing else.
 
Share this answer
 
Comments
Member 11306487 29-Jul-16 8:18am    
yes i understand your answer but i don't do it because i am new in asp.net and c#.and this code was copied form some video tutorial.but this can't meet my requirements.can you please show me how i do loop method for sql query

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900