Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys!

I want to Bulk Copy a .CSV file and save it to db. Everything is going fine but the problem I am facing is that the data of one of the column gets slightly changed. A value of "SS13" becomes "13.0000" after it is copied.
Please help me to fix it friends.
Here is my code ..

C#
string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + dirCSV.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
                string sql_select;
                OdbcConnection conn;
                conn = new OdbcConnection(strConnString.Trim());
                conn.Open();
                sql_select = "select * from [" + FileNevCSV.Trim() + "]";
                //Creates the data adapter
                OdbcDataAdapter obj_oledb_da = new OdbcDataAdapter(sql_select, conn);
                OdbcCommand myCommand = new OdbcCommand(sql_select, conn);
                //Fills dataset with the records from CSV file
                DataSet ds = new DataSet();
                obj_oledb_da.Fill(ds, "csv");
                OdbcDataReader myReader;
                DeleteAllFromTempStock();
                myReader = myCommand.ExecuteReader();
                SqlBulkCopy stockBulkCopy = new    SqlBulkCopy(ConfigurationManager.ConnectionStrings["companyConnectionString"].ConnectionString);
                stockBulkCopy.DestinationTableName = "TemporaryStock";
                stockBulkCopy.WriteToServer(myReader);
                //closes the connection
                myReader.Close();
                conn.Close();


[edit]code block added [/edit]
Posted
Updated 14-Mar-13 23:36pm
v5
Comments
[no name] 13-Mar-13 16:31pm    
Well check the data type of your columns.
tipu khan jan 13-Mar-13 16:36pm    
There is nothing to do with DataType because the error remains during run time as i checked it in dataset visualizer before saving it to db...
[no name] 13-Mar-13 16:40pm    
Uhm.... no it has everything to do with data type.
tipu khan jan 13-Mar-13 16:50pm    
Sir, the datatype is varchar for this column .. but actually the value gets changed when the csv file is read .. i observed it in dataset visualizer ..
R. Giskard Reventlov 13-Mar-13 16:42pm    
What data type is the column in the database? Only asking as you have ignored ThePahntomUpvoter for unknown reasons.

1 solution

You might be interested in the TextFieldParser[^]

using (TextFieldParser parser = new TextFieldParser(filename))
{
    parser.CommentTokens = new string[] { "#" };
    parser.SetDelimiters(new string[] { ";" });
    parser.HasFieldsEnclosedInQuotes = true;
 
    // Skip the header
    parser.ReadLine();
 
    while (!parser.EndOfData)
    {
        string[] fields = parser.ReadFields();
        // process the row
        ....
    }
}


This way you can handle the conversion of each field yourself.

Best regards
Espen Harlinn
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900