title: Insert data into SQL Server database from an Excel file
body: <p>I'm trying to insert data to database using excel file. This code works fine for me. But I used this with windows form application. How can I change this code to WCF? I need to open Excel file using Windows Forms application and then pass the value to WCF service to insert data into the database. How can I do this?</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
  {
      OpenFileDialog opn = new OpenFileDialog();
      opn.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
  
      if (opn.ShowDialog() == DialogResult.Cancel)
          return;
  
      try { 
          FileStream strm = new FileStream(opn.FileName, FileMode.Open);
          IExcelDataReader excldr = ExcelReaderFactory.CreateOpenXmlReader(strm);
  
          DataSet rslt = excldr.AsDataSet();
  
          DataClasses1DataContext conn = new DataClasses1DataContext();
  
          foreach (DataTable table in rslt.Tables)
          {
              foreach (DataRow dr in table.Rows)
              {
                  tblExcel addTbl = new tblExcel()
                                    {
                                      SID = Convert.ToString(dr[0]),
                                      Name = Convert.ToString(dr[1]),
                                      Address = Convert.ToString(dr[2])
                                    };
                  conn.tblExcels.InsertOnSubmit(addTbl);
              }
          }
  
          conn.SubmitChanges();
  
          excldr.Close();
          strm.Close();
  
          MessageBox.Show("successfully");
      }
      catch (IOException x)
      {
          MessageBox.Show(x.Message);
      }
  }
  </code></pre>