开发者

savepath? saving excel to sql?

开发者 https://www.devze.com 2023-01-11 06:14 出处:网络
I\'m really confused on how to go about doing this. I want to be able to upload an excel sheet from my web app using the

I'm really confused on how to go about doing this.

  • I want to be able to upload an excel sheet from my web app using the file upload control.
  • Next, I want to read each individual row under the first row. (So starting from row 2, row 1 will be the column title).
  • Finally, I want to pass the Strings I've read into another method that'll do what I want with it and then post to a gridview.

How I intend to do so...

Since I have my web app published on the network, hosted on my local box... my usual savepath for the file uploaded (desktop) doe开发者_JAVA技巧sn't work.

So I thought to save it onto a SQL Server that is also hosted on my local box.

Therefore, I guess I'm trying to:

  • Save the uploaded excel into a SQL database.
  • Read each line from the excel and pass it through the intended method.

Okay, that was super confusing. There must be an easier way to do this! (Do I really need a SQL database?)

Oh, and any good ideas for my savePath?


Haven't really solved this. But it's easier to read whatever's on the excel file using the OleDBConnection like this:

            try
            {
                using (OleDbConnection olcon = new OleDbConnection())
                {
                        olcon.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(fileupFile.FileName) + ";Extended Properties=Excel 12.0";
                        OleDbCommand olcmd = new OleDbCommand("SELECT * FROM [Sheet1$]", olcon);
                        olcon.Open();
                        OleDbDataReader olread = olcmd.ExecuteReader();
                        while (olread.Read())
                        {
                            line = (String)(olread.GetString(0));
                            Verify(line); //calls the datatosql method
                        }
                        olcon.Close();
                        lblFiup.Text = "Data Inserted Sucessfully";   
                        lblFiup.ForeColor = System.Drawing.Color.Green; 
                }
            }

Then, pass into another method, which uses SqlConnection to write into SQL Server. Like this:

        protected void datatosql(String url, String stat)
        {
            try
            {
                using (SqlConnection sqlcon = new SqlConnection("Server=(local);Database=URLs;Trusted_Connection=True"))
                {
                    using (SqlCommand sqlcom = sqlcon.CreateCommand())
                    {
                        sqlcom.CommandText = "INSERT INTO WEVRecordsTest (URL, Status) VALUES ('" + url + "','" + stat + "')";
                        sqlcon.Open();
                        sqlcom.ExecuteNonQuery();
                        sqlcon.Close();
                    }
                }
            }
            catch (SqlException se)
            {
                lblHist.Text = se.Message;
                lblHist.ForeColor = System.Drawing.Color.Red;
            }
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消