SQLBulkCopy C#错误(字段值更改) [英] SQLBulkCopy C# Error (Change in Field Value)
本文介绍了SQLBulkCopy C#错误(字段值更改)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello Guys!
我想批量复制.CSV文件并将其保存到db。一切都很顺利,但我面临的问题是其中一个列的数据略有变化。复制后,SS13的值变为13.0000。
请帮我修好朋友。
这是我的代码..
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 ..
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]已添加代码块[/ edit]
推荐答案
您可能对 TextFieldParser [ ^ ]
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
....
}
}
这样你就可以自己处理每个领域的转换。
祝你好运
Espen Harlinn
This way you can handle the conversion of each field yourself.
Best regards
Espen Harlinn
这篇关于SQLBulkCopy C#错误(字段值更改)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文