我的代码中出现此错误: {"更新条目时发生错误。有关详细信息,请参阅内部异常。"}
我看到内部异常并且: {"参数值' 99999,999'超出范围。"}
但是当我只上传一个档案时,他很好,当我上传多个档案时,他会抛出异常。
这是我的代码:
private void ProcessarCEP(int i,FileInfo file,string [] Lines)
{
string vNomeArquivo = string.Empty;
DBCEPEntities DBCEP = new DBCEPEntities();
DirectoryInfo info = new DirectoryInfo(file.DirectoryName);
FileInfo[] files = info.GetFiles("*.txt", SearchOption.AllDirectories);
int vTamanhoBase = DBCEP.CAD_CEP.Where(t => t.UF == file.Name.Substring(15,2)).Count();
for (int a = vTamanhoBase; a < i + vTamanhoBase; a++)
{
if (a >= Lines.Count()) break;
CAD_CEP CEP = new CAD_CEP();
string[] Registros = Lines[a].Split('@');
CEP.CEP = Convert.ToDecimal(Registros[7]);
CEP.UF = Registros[1];
CEP.LOGRADOURO = Registros[8];
CEP.ENDERECO = Registros[5];
if (Registros[2] != string.Empty)
{
decimal CodLocal = Convert.ToDecimal(Registros[2]);
var local = DBCEP.CAD_CIDADE.Where(c => c.CODLOCAL == CodLocal).FirstOrDefault();
CEP.LOCAL = local.LOCAL;
}
if (Registros[3] != string.Empty)
{
decimal CodBairro = Convert.ToDecimal(Registros[3]);
var bairro = DBCEP.CAD_BAIRRO.Where(c => c.CODBAIRRO == CodBairro).FirstOrDefault();
CEP.BAIRRO = bairro.BAIRRO;
}
vNomeArquivo = ConfigurationManager.AppSettings["NomeArquivoSequencia"].ToString();
if (files.Where(f => f.Name.ToUpper() == vNomeArquivo.ToUpper()).Count() > 0)
{
if (Registros[0] != string.Empty)
{
string[] LinesSec = File.ReadAllLines(files.Where(f => f.Name.ToUpper().StartsWith(vNomeArquivo.ToUpper())).FirstOrDefault().FullName.ToString(), Encoding.Default);
string vLinhaSec = LinesSec.Where(l => l.StartsWith(Registros[0] + "@")).FirstOrDefault();
if (vLinhaSec != null)
{
string[] vLinhaSplit = vLinhaSec.Split('@');
if (vLinhaSplit[1] != string.Empty)
{
CEP.DE = Convert.ToDecimal(vLinhaSplit[1]);
}
if (vLinhaSplit[2] != string.Empty)
{
CEP.ATE = Convert.ToDecimal(vLinhaSplit[2]);
}
CEP.FL_PAR_IMPAR = vLinhaSplit[3];
}
Registros = null;
LinesSec = null;
}
}
DBCEP.CAD_CEP.Add(CEP);
}
DBCEP.SaveChanges();
vTamanhoBase = DBCEP.CAD_CEP.Where(t => t.UF == file.Name.Substring(15, 2)).Count();
DBCEP.Dispose();
GC.WaitForPendingFinalizers();
GC.Collect();
if (Lines.Count() <= i + vTamanhoBase)
{
if(vTamanhoBase != Lines.Count())
{
ProcessarCEP(Lines.Count() - i, file, Lines);
}
}
else
{
if(vTamanhoBase != Lines.Count())
{
ProcessarCEP(i, file, Lines);
}
}
}