我有一个Windows窗体应用程序,用户在其中选择.csv文件并单击导入。此时,文件应使用SqlBulkCopy导入SQL Server。 问题是我遇到了一些错误,我认为这是由于导入时字段为空。我收到这个错误:
$tmp = $_FILES['file1']['tmp_name'];
$name = $_FILES['file1']['name'];
$path = getcwd() . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $name;
$success = move_uploaded_file($tmp, $path);
if($success){
$query = "INSERT INTO phptbl (Name, Photo) VALUES ('name', :path)";
$stmt = $db->prepare();
$stmt->execute([':path' => $path]);
}else{
echo 'Something went wrong!';
}
这是CSV的样本
Input string was not in a correct format.Couldn't store <> in ScranDrugQuantity Column. Expected type is Int32.
下面是我尝试导入的代码:
974524,416194,131179,For information only. ,17/05/2016 16:43:27,17/05/2016 16:43:27,1,xxxx xxxxx,xxxx xxxxx,1,Information about,930720,,320139002,1,3317411000001100,,1252,,5143,17/05/2016 16:42:51,17/05/2016 00:00:00,17/05/2016 16:43:27,0051431705161623286,3348074428,HS21X,,,
974522,416192,153168,information ,17/05/2016 16:42:54,17/05/2016 16:42:54,500,xxxxx xxxxxx,xxxxxxxx ,80,more information,930718,,,,,,,737,,4256,17/05/2016 16:42:13,17/05/2016 00:00:00,17/05/2016 16:42:54,31944C7C3AC04C4D92204775BBCEA365,3418645550,XX21,,,
974521,416192,153168,xxxxxxxxxxxxx ,17/05/2016 16:42:49,17/05/2016 16:42:49,500,xxxxxxxx xxxxxxxxx,xxxxxxxx,80,xxxxxxxx,930717,,,,,,,737,,4256,17/05/2016 16:42:13,17/05/2016 00:00:00,17/05/2016 16:42:49,31944C7C3AC04C4D92204775BBCEA365,3418645550,HS21,,,
有没有办法在读入CSV时插入默认值,或者有什么方法可以阻止此错误?
由于
答案 0 :(得分:1)
您应该将sLine
数组中的空字符串转换为空值,否则DataTable
将尝试将值转换为为列定义的数据类型。例如使用LINQ:
Dim sLine As String() = oSR.ReadLine() _
.Split(CChar(",")) _
.Select(Function(x) If(String.IsNullOrEmpty(x), Nothing, x)) _
.ToArray()