C# 行动 等待延迟“000:00:00.400” 结束尝试 开始捕捉 回滚事务 RAISERROR(N‘将数据从临时表移动到主表时出错’,--消息文本。 1. 1); 打印“失败,错误:”+错误消息() 端接
使用.Net ReadAllLines()方法将整个文件读入字符串数组对象,然后运行并行For循环以并行处理所有行C# 行动 等待延迟“000:00:00.400” 结束尝试 开始捕捉 回滚事务 RAISERROR(N‘将数据从临时表移动到主表时出错’,--消息文本。 1. 1); 打印“失败,错误:”+错误消息() 端接,c#,entity-framework,bulkinsert,excel-import,C#,Entity Framework,Bulkinsert,Excel Import,使用.Net ReadAllLines()方法将整个文件读入字符串数组对象,然后运行并行For循环以并行处理所有行 private bool ProcessFile(string FolderPath, string FileExtension) { try { //all files with requisite file extension DirectoryInfo dinfo = new DirectoryInfo(FolderPath);
private bool ProcessFile(string FolderPath, string FileExtension)
{
try
{
//all files with requisite file extension
DirectoryInfo dinfo = new DirectoryInfo(FolderPath);
FileInfo[] Files = dinfo.GetFiles(FileExtension);
foreach (FileInfo file in Files)
{
List<String> AllLines = new List<String>();
using (StreamReader sr = File.OpenText(file.FullName))
{
int x = 0;
while (!sr.EndOfStream)
{
AllLines.Add(sr.ReadLine());
x += 1;
}
sr.Close();
}
Parallel.For(0, AllLines.Count, x =>
{
InsertDataCheck(AllLines[x]);
});
}
GC.Collect();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return false;
}
private void InsertDataCheck(string Line)
{
//check if you want to insert data on the basis of your condition
//and then insert your data
}
private bool ProcessFile(字符串FolderPath,字符串文件扩展名)
{
尝试
{
//具有必需文件扩展名的所有文件
DirectoryInfo dinfo=新的DirectoryInfo(FolderPath);
FileInfo[]Files=dinfo.GetFiles(文件扩展名);
foreach(文件中的文件信息文件)
{
List AllLines=新列表();
使用(StreamReader sr=File.OpenText(File.FullName))
{
int x=0;
而(!sr.EndOfStream)
{
添加(sr.ReadLine());
x+=1;
}
高级关闭();
}
Parallel.For(0,AllLines.Count,x=>
{
InsertDataCheck(所有行[x]);
});
}
GC.Collect();
返回true;
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
返回false;
}
私有void InsertDataCheck(字符串行)
{
//检查是否要根据您的条件插入数据
//然后插入数据
}
你能告诉我有多慢吗?谢谢@Higgins,我现在很忙,稍后再试,最大记录不会超过5000条。谢谢@Ajay,我现在很忙,稍后再试。
-- our main loop
WHILE (@min <= @TotalRECORD)
BEGIN
IF OBJECT_ID('tempdb..#EntityIds') IS NOT NULL
DROP TABLE #EntityIds
IF OBJECT_ID('tempdb..#RefNumRepository') IS NOT NULL
DROP TABLE #RefNumRepository
IF OBJECT_ID('tempdb..#ActorIds') IS NOT NULL
DROP TABLE #ActorIds
IF OBJECT_ID('tempdb..#SecondaryActorIds') IS NOT NULL
DROP TABLE #SecondaryActorIds
CREATE TABLE #EntityIds (
pkid INT identity(1, 1) NOT NULL
,mid INT
,eid INT
)
CREATE TABLE #ActorIds (
pkid INT identity(1, 1) NOT NULL
,mid INT
,aid INT
)
CREATE TABLE #SecondaryActorIds (
pkid INT identity(1, 1) NOT NULL
,mid INT
,aid INT
)
CREATE TABLE #RefNumRepository (
pkid INT identity(1, 1) NOT NULL
,RefNum VARCHAR(50)
)
BEGIN TRANSACTION
BEGIN TRY
UPDATE TOP (@RecordsPerLoop + 1) RefNumRepository
SET IsUsed = 1
OUTPUT deleted.RefNum
INTO #RefNumRepository(RefNum)
WHERE IsUsed = 0;
PRINT 'Entities'
INSERT INTO Entities (
ModifiedBy
,ModifiedDate
,RecordOwnerName
,IsConflictOfInterest
,FKClaimantTypeId
,OtherClaimantType
,InstitutionAccountNumber
,RefNum
,FKSubmitTypeId
,FKClaimStatusTypeId
,RecordType
,ClaimNum
,FilingDate
,FirstName
,Lastname
,Email
,SSN
,Source
,ClaimDataCertifiedDate
)
OUTPUT Inserted.pkid
,Inserted.source
INTO #EntityIds(eid, mid)
SELECT @ModifiedBy
,@ModifiedDate
,NULL
,1
,CASE
WHEN IsNull(company, '') = ''
THEN @IndividualClaimantType
ELSE @CompanyClaimantType
END
,NULL
,NULL
,''
,@SubmitTypeId
,@ClaimStatusId
,'CM'
,NULL
,@ModifiedDate
,IsNull(fname, '')
,IsNull(lname, '')
,IsNull(Email, '')
,IsNull(ssn, '')
,rawID
,@ModifiedDate
FROM TempClassMemberRecords
WHERE rawID BETWEEN @min
AND @max
AND IsProcessed IS NULL
EXEC dbo.[USP_AssignClassMemberRefNums] @startingPKId
PRINT 'Actors'
-- bulk insert our range of class members into Actors while inserting the primary key into our temp table
INSERT INTO Actors (
FKActorTypeId
,ModifiedBy
,ModifiedDate
,LastName
,FirstName
,MiddleName
,CommPreference
,IsPayee
,IsUSCitizen
,ein
,ssn
,company
,attention
,NotificationsBlocked
,SearchName
,ClientAcctNumber
)
OUTPUT Inserted.pkid
,inserted.attention
INTO #ActorIds(aid, mid)
SELECT @PrimaryActorTypeId
,@ModifiedBy
,@ModifiedDate
,IsNull(lname, '')
,IsNull(fname, '')
,''
,IsNull(@DefaultCommPreference, 'Mail')
,1
,NULL
,IsNull(ein, '')
,IsNull(ssn, '')
,IsNull(company, '')
,rawid
,0
,CASE WHEN len(ISNULL(company, '')) > 0 THEN company
ELSE
CASE WHEN (len(ISNULL(lname, '')) > 0 OR len(ISNULL(fname, '')) > 0)
THEN lname + ', ' + fname
ELSE ''
END
END
,ACCTNUM
FROM TempClassMemberRecords
WHERE (
isnull(company, '') <> ''
OR isNull(fname, '') <> ''
OR isNull(lname, '') <> ''
)
AND rawid BETWEEN @Min
AND @Max
AND IsProcessed IS NULL
PRINT 'Entities2Actors'
-- bulk insert the relations of Entities to Actors in Entities2Actors
INSERT INTO Entities2Actors (
FKEntityId
,FKActorId
,IsActorBeneficiary
,ModifiedBy
,ModifiedDate
)
SELECT e.eid
,a.aid
,1
,@ModifiedBy
,@ModifiedDate
FROM #EntityIds e
INNER JOIN #ActorIds a ON e.mid = a.mid
-- etc...
PRINT 'Addressed'
--Bulk Insert into Address table for Primary Actor Address
INSERT INTO Addresses (
FKActorId
,ModifiedBy
,ModifiedDate
,Address1
,Address2
,City
,STATE
,Zip
,Zip4
,FKCountryId
)
SELECT a.aid
,@ModifiedBy
,@ModifiedDate
,IsNull(Address, '')
,IsNull(Address2, '')
,IsNull(City, '')
,IsNull([State], '')
,IsNull(Zip, '')
,IsNull(Zip4, '')
,ISNULL(@PrimaryCountryId, @DefaultCountryId)
FROM #ActorIds a
INNER JOIN TempClassMemberRecords c ON a.mid = c.rawId
-- etc...
UPDATE tempClassMemberRecords
SET IsProcessed = 1
WHERE rawid BETWEEN @Min
AND @Max
AND IsProcessed IS NULL
SET @Min = @max + 1
SET @max = @max + @RecordsPerloop
COMMIT TRANSACTION
WAITFOR DELAY '000:00:00.400'
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
RAISERROR (N'Error in moving data from Temporary table to Main tables.', -- Message text.
1,
1);
PRINT 'Failed with error: ' + ERROR_MESSAGE()
END CATCH
private bool ProcessFile(string FolderPath, string FileExtension)
{
try
{
//all files with requisite file extension
DirectoryInfo dinfo = new DirectoryInfo(FolderPath);
FileInfo[] Files = dinfo.GetFiles(FileExtension);
foreach (FileInfo file in Files)
{
List<String> AllLines = new List<String>();
using (StreamReader sr = File.OpenText(file.FullName))
{
int x = 0;
while (!sr.EndOfStream)
{
AllLines.Add(sr.ReadLine());
x += 1;
}
sr.Close();
}
Parallel.For(0, AllLines.Count, x =>
{
InsertDataCheck(AllLines[x]);
});
}
GC.Collect();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return false;
}
private void InsertDataCheck(string Line)
{
//check if you want to insert data on the basis of your condition
//and then insert your data
}