SqlBulkCopy - The given value of type String from the data source cannot be converted to type money of the specified target column

后端 未结 8 1863
慢半拍i
慢半拍i 2020-11-30 03:08

I\'m getting this exception when trying to do an SqlBulkCopy from a DataTable.

Error Message: The given value of type String from the data source cannot be c         


        
相关标签:
8条回答
  • 2020-11-30 03:15

    For the people stumbling across this question and getting a similar error message in regards to an nvarchar instead of money:

    The given value of type String from the data source cannot be converted to type nvarchar of the specified target column.

    This could be caused by a too-short column.

    For example, if your column is defined as nvarchar(20) and you have a 40 character string, you may get this error.

    Source

    0 讨论(0)
  • 2020-11-30 03:17

    There is another issue you have to take care of it when you try mapping column which is string length, for example TK_NO nvarchar(50) you will have to map to the same length as the destination field.

    0 讨论(0)
  • 2020-11-30 03:17

    Check The data you are writing to Server. May be data has delimiter which is not used.

    like

    045|2272575|0.000|0.000|2013-10-07
    045|2272585|0.000|0.000;2013-10-07
    

    your delimiter is '|' but data has a delimiter ';'. So for this you are getting the error.

    0 讨论(0)
  • 2020-11-30 03:21

    Not going to be everyone's fix, but it was for me:

    So, i ran across this exact issue. The problem I seemed to have was when my DataTable didnt have an ID column, but the target destination had one with a primary key.

    When i adapted my DataTable to have an id, the copy worked perfectly.

    In my scenario, the Id column isnt very important to have the primary key so i deleted this column from the target destination table and the SqlBulkCopy is working without issue.

    0 讨论(0)
  • 2020-11-30 03:24

    Please use SqlBulkCopyColumnMapping.

    Example:

    private void SaveFileToDatabase(string filePath)
    {
        string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["MHMRA_TexMedEvsConnectionString"].ConnectionString.ToString();
    
        String excelConnString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0\"", filePath);
        //Create Connection to Excel work book 
        using (OleDbConnection excelConnection = new OleDbConnection(excelConnString))
        {
            //Create OleDbCommand to fetch data from Excel 
            using (OleDbCommand cmd = new OleDbCommand("Select * from [Crosswalk$]", excelConnection))
            {
                excelConnection.Open();
                using (OleDbDataReader dReader = cmd.ExecuteReader())
                {
                    using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
                    {
                        //Give your Destination table name 
                        sqlBulk.DestinationTableName = "PaySrcCrosswalk";
    
                        // this is a simpler alternative to explicit column mappings, if the column names are the same on both sides and data types match
                        foreach(DataColumn column in dt.Columns) {
                             s.ColumnMappings.Add(new SqlBulkCopyColumnMapping(column.ColumnName, column.ColumnName));
                         }
                       
                        sqlBulk.WriteToServer(dReader);
                    }
                }
            }
        }
    }  
    
    0 讨论(0)
  • 2020-11-30 03:25

    @Corey - It just simply strips out all invalid characters. However, your comment made me think of the answer.

    The problem was that many of the fields in my database are nullable. When using SqlBulkCopy, an empty string is not inserted as a null value. So in the case of my fields that are not varchar (bit, int, decimal, datetime, etc) it was trying to insert an empty string, which obviously is not valid for that data type.

    The solution was to modify my loop where I validate the values to this (repeated for each datatype that is not string)

    //--- convert decimal values
    foreach (DataColumn DecCol in DecimalColumns)
    {
         if(string.IsNullOrEmpty(dr[DecCol].ToString()))
              dr[DecCol] = null; //--- this had to be set to null, not empty
         else
              dr[DecCol] = Helpers.CleanDecimal(dr[DecCol].ToString());
    }
    

    After making the adjustments above, everything inserts without issues.

    0 讨论(0)
提交回复
热议问题