Excel to SQL Server

Last post 07-04-2009 3:19 PM by maverickhyd. 1 replies.

Sort Posts:

  • Excel to SQL Server

    07-04-2009, 6:20 AM
    • Member
      3 point Member
    • bubbly
    • Member since 03-20-2008, 6:40 AM
    • Posts 52

    hi all,

    I ant to transfer Excel data to SQL server 2005, i have done it using this code ----

    OleDbConnection ExcelCon = ExcelConnection(FileName);
    OleDbCommand ExcelCmd = new OleDbCommand("select * from [sheet1$]", ExcelCon);
    ExcelCon.Open();
                
    IDataReader eRdr = ExcelCmd.ExecuteReader();
    DataTable tb = new DataTable();
           
    tb.Load(eRdr);
    SqlConnection myConnection = getSqlConnection();
    myConnection.Open();

    SqlBulkCopy bulk = new SqlBulkCopy(myConnection);
    bulk.DestinationTableName = ctrlDestination.SelectedValue;

            if(Session["columnmap"] != null)
            {
                ColumnMapping = Session["columnmap"] as Hashtable;
                foreach (DictionaryEntry de in ColumnMapping)
                {
                   
                    bulk.ColumnMappings.Add(de.Key.ToString(), de.Value.ToString());
                   
                }
            }
            try
            {

               bulk.WriteToServer(tb);}


    But im getting exception as "pload Data The given value of type String from the data source cannot be converted to type smallint of the specified target column."

    Wats the problem, plz suggest solution


    Thank you,

    Thanks & Regards
    -Bubbly
  • Re: Excel to SQL Server

    07-04-2009, 3:19 PM
    Answer
    • Contributor
      2,387 point Contributor
    • maverickhyd
    • Member since 03-25-2009, 6:38 AM
    • Posts 416

     Hi Bubly,

    Check your Destination Table(SQL) must match columns in Excel sheet.

    The exception saying that

    you are sending string data from excel and sql database datataype given as int

    Please Mark as Answer if it helped You!
Page 1 of 1 (2 items)