Write data from excel file to a table in SQL Database

Andy,

The next step is to take the info from the Excel file and write it to a table in SQL DB.
GoldenJagAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GoldenJagAuthor Commented:
This is officially what my code looks like:

private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
      int fileid = Convert.ToInt32(DataGrid1.SelectedItem.Cells[1].Text);
      com.solu.webservices.Crypt ws = new com.solu.webservices.Crypt();
      byte[] test = ws.DecryptStoredFile(pm.sessionKey,fileid);
                  
      //WRITE TO TEMP FILE
      //string FileName = Path.GetTempFileName();
      string FileName = @"C:\temp.xls";
      FileStream File_Stream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
      try
      {
            File_Stream.Write(test,0,test.Length);
      }
      catch(Exception ex)
      {
            Response.Write(ex.Message.ToString());
      }
      finally
      {
            File_Stream.Close();
      }

                                                  //NEXT STEP:
                  //WRITE to info from Excel file to SQL  Table via ODBC

AGBrownCommented:
OK, cool.

Is this the new code? You should be careful with the FileStream object ... the using {} block we had is a better way of doing it as it ensures that the stream is both flushed and disposed once you've finished.

Other than that, you now want to write from Excel to SQL, unfortunately I haven't had much success with this in the past. ODBC and Excel is a bit of a nightmare - the data-type detection is very dodgy.

However, I can point you to this place to get you started: http://www.codeproject.com/csharp/Excel_using_OLEDB.asp

Hopefully that will help, or someone else will join in this question to add some more advice. Feel free to ask for more details as you go.

Andy
GoldenJagAuthor Commented:
I found the solution: it is

string strConn;

                  strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                        "Data Source= C:\\temp.xls;" +
                        "Extended Properties=Excel 8.0;";
                  //You must use the $ after the object you reference in the spreadsheet
                  OleDbConnection objConn = new OleDbConnection(strConn);
            objConn.Open();
                  OleDbCommand myCommand = new OleDbCommand("SELECT * FROM [Sheet1$]",objConn);
                  
                  OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

                  objAdapter1.SelectCommand = myCommand;

                  DataSet myDataSet = new DataSet();

                  objAdapter1.Fill(myDataSet, "XLData");
                  DataGrid1.DataSource = myDataSet.Tables[0].DefaultView;
                  DataGrid1.DataBind();
AGBrownCommented:
Fine by me
GranModCommented:
Closed, 500 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.