我有一个C#应用程序,它只解析给定网络目录中的所有txt文档,并将数据导入SQL服务器数据库。一切都在巡航,直到大约1800个文件发生时,在列中有一些空白被称为DBType.Decimal(并且文件中的值通常为零,而不是空白)。所以我得到了这个错误,“无法将nvarchar转换为十进制”。我想知道我怎么能告诉应用程序只是跳过有这个问题的行?也许我甚至可以将列类型更改为varchar,即使值是数字(这会产生什么问题?)感谢您的帮助!
using System;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
namespace JohnDeereCMMDataParser
{
internal class Program
{
public static List<string> GetImportedFileList()
{
List<string> ImportedFiles = new List<string>();
using (SqlConnection connect = new SqlConnection(@"Server=FRXSQLDEV;Database=RX_CMMData;Integrated Security=YES"))
{
connect.Open();
using (SqlCommand fmd = connect.CreateCommand())
{
fmd.CommandText = @"SELECT FileName FROM CMMData;";
fmd.CommandType = CommandType.Text;
SqlDataReader r = fmd.ExecuteReader();
while (r.Read())
{
ImportedFiles.Add(Convert.ToString(r["FileName"]));
}
}
}
return ImportedFiles;
}
private static void Main(string[] args)
{
Console.Title = "John Deere CMM Data Parser";
Console.WriteLine("Preparing CMM Data Parser... done");
Console.WriteLine("Scanning for new CMM data...");
Console.ForegroundColor = ConsoleColor.Gray;
using (SqlConnection con = new SqlConnection(@"Server=FRXSQLDEV;Database=RX_CMMData;Integrated Security=YES"))
{
con.Open();
using (SqlCommand insertCommand = con.CreateCommand())
{
Console.WriteLine("Connecting to SQL server...");
SqlCommand cmdd = con.CreateCommand();
string[] files = Directory.GetFiles(@"C:\Documents and Settings\js91162\Desktop\CMM WENZEL\", "*_*_*.txt", SearchOption.AllDirectories);
List<string> ImportedFiles = GetImportedFileList();
insertCommand.Parameters.Add(new SqlParameter("@FeatType", DbType.String));
insertCommand.Parameters.Add(new SqlParameter("@FeatName", DbType.String));
insertCommand.Parameters.Add(new SqlParameter("@Axis", DbType.String));
insertCommand.Parameters.Add(new SqlParameter("@Actual", DbType.Decimal));
insertCommand.Parameters.Add(new SqlParameter("@Nominal", DbType.Decimal));
insertCommand.Parameters.Add(new SqlParameter("@Dev", DbType.Decimal));
insertCommand.Parameters.Add(new SqlParameter("@TolMin", DbType.Decimal));
insertCommand.Parameters.Add(new SqlParameter("@TolPlus", DbType.Decimal));
insertCommand.Parameters.Add(new SqlParameter("@OutOfTol", DbType.Decimal));
foreach (string file in files.Except(ImportedFiles))
{
var FileNameExt1 = Path.GetFileName(file);
cmdd.Parameters.Clear();
cmdd.Parameters.Add(new SqlParameter("@FileExt", FileNameExt1));
cmdd.CommandText =
@"
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'RX_CMMData' AND TABLE_NAME = 'CMMData')) BEGIN SELECT COUNT(*) FROM CMMData WHERE FileName = @FileExt; END";
int count = Convert.ToInt32(cmdd.ExecuteScalar());
con.Close();
con.Open();
if (count == 0)
{
Console.WriteLine("Preparing to parse CMM data for SQL import...");
if (file.Count(c => c == '_') > 5) continue;
insertCommand.CommandText =
@"
INSERT INTO CMMData (FeatType, FeatName, Axis, Actual, Nominal, Dev, TolMin, TolPlus, OutOfTol, PartNumber, CMMNumber, Date, FileName)
VALUES (@FeatType, @FeatName, @Axis, @Actual, @Nominal, @Dev, @TolMin, @TolPlus, @OutOfTol, @PartNumber, @CMMNumber, @Date, @FileName);";
string FileNameExt = Path.GetFullPath(file);
string RNumber = Path.GetFileNameWithoutExtension(file);
int index2 = RNumber.IndexOf("~");
Match RNumberE = Regex.Match(RNumber, @"^(R|L)\d{6}(COMP|CRIT|TEST|SU[1-9])(?=_)", RegexOptions.IgnoreCase);
Match RNumberD = Regex.Match(RNumber, @"(?<=_)\d{3}[A-Z]\d{4}|\d{3}[A-Z]\d\w\w\d(?=_)", RegexOptions.IgnoreCase);
Match RNumberDate = Regex.Match(RNumber, @"(?<=_)\d{8}(?=_)", RegexOptions.IgnoreCase);
string RNumE = Convert.ToString(RNumberE);
string RNumD = Convert.ToString(RNumberD);
if (RNumberD.Value == @"") continue;
if (RNumberE.Value == @"") continue;
if (RNumberDate.Value == @"") continue;
if (index2 != -1) continue;
DateTime dateTime = DateTime.ParseExact(RNumberDate.Value, "yyyyMMdd", Thread.CurrentThread.CurrentCulture);
string cmmDate = dateTime.ToString("dd-MMM-yyyy");
string[] lines = File.ReadAllLines(file);
bool parse = false;
foreach (string tmpLine in lines)
{
string line = tmpLine.Trim();
if (!parse && line.StartsWith("Feat. Type,"))
{
parse = true;
continue;
}
if (!parse || string.IsNullOrEmpty(line))
{
continue;
}
Console.WriteLine(tmpLine);
foreach (SqlParameter parameter in insertCommand.Parameters)
{
parameter.Value = null;
}
string[] values = line.Split(new[] { ',' });
for (int i = 0; i < values.Length - 1; i++)
{
if (i = "" || i = null) continue;
SqlParameter param = insertCommand.Parameters[i];
if (param.DbType == DbType.Decimal)
{
decimal value;
param.Value = decimal.TryParse(values[i], out value) ? value : 0;
}
else
{
param.Value = values[i];
}
}
insertCommand.Parameters.Add(new SqlParameter("@PartNumber", RNumE));
insertCommand.Parameters.Add(new SqlParameter("@CMMNumber", RNumD));
insertCommand.Parameters.Add(new SqlParameter("@Date", cmmDate));
insertCommand.Parameters.Add(new SqlParameter("@FileName", FileNameExt));
insertCommand.ExecuteNonQuery();
insertCommand.Parameters.RemoveAt("@PartNumber");
insertCommand.Parameters.RemoveAt("@CMMNumber");
insertCommand.Parameters.RemoveAt("@Date");
insertCommand.Parameters.RemoveAt("@FileName");
}
}
}
Console.WriteLine("CMM data successfully imported to SQL database...");
}
con.Close();
}
}
}
}
答案 0 :(得分:2)
空白显然不会被解析为Decimal,因此您可能必须在数据库中定义默认值0.0,或检查输入并用0.0替换任何空格。不要试图通过更改数据类型来解决问题,通过对输入进行传输来解决问题。