C# 使用相同的进度报告回调。。。除非我真的误解了发生的事情,否则我已经更新了答案。据我所知,问题在于报告进度,而不是提高数据读取/加载的性能。如果不是这样,作者可以让我们知道:)@Phil P,我读了你的评论,你的权利,我先将数据加载到数据表,然后将数据表发送
C# 使用相同的进度报告回调。。。除非我真的误解了发生的事情,否则我已经更新了答案。据我所知,问题在于报告进度,而不是提高数据读取/加载的性能。如果不是这样,作者可以让我们知道:)@Phil P,我读了你的评论,你的权利,我先将数据加载到数据表,然后将数据表发送,c#,sql-server,console-application,C#,Sql Server,Console Application,使用相同的进度报告回调。。。除非我真的误解了发生的事情,否则我已经更新了答案。据我所知,问题在于报告进度,而不是提高数据读取/加载的性能。如果不是这样,作者可以让我们知道:)@Phil P,我读了你的评论,你的权利,我先将数据加载到数据表,然后将数据表发送到sql。更新数据需要20分钟的时间。我将尝试你的解决方案并让你知道。@Virat。你没有对这件事发表任何评论,。但是使用Parallel.ForEach可以设置控制台。在正文部分写入,以跟踪进度。 ALTER PROCEDURE [dbo].
使用相同的进度报告回调。。。除非我真的误解了发生的事情,否则我已经更新了答案。据我所知,问题在于报告进度,而不是提高数据读取/加载的性能。如果不是这样,作者可以让我们知道:)@Phil P,我读了你的评论,你的权利,我先将数据加载到数据表,然后将数据表发送到sql。更新数据需要20分钟的时间。我将尝试你的解决方案并让你知道。@Virat。你没有对这件事发表任何评论,。但是使用
Parallel.ForEach
可以设置控制台。在正文部分写入,以跟踪进度。
ALTER PROCEDURE [dbo].[sp_Data_Inserting_In_To_QueueTable]
-- Add the parameters for the stored procedure here
@Values AS [dbo].[Type_Table] READONLY
AS
BEGIN
SET NOCOUNT ON;
;WITH CTE_data_codes AS(
select [NO], [ID], [Name], [Code] from @SearchValues where [No] != '0'
)
MERGE tbl__QueueTable_Codes AS t
-- Values is the temp table in which data is coming from the console application
USING CTE_data_codes AS s
ON (s.Code = t.Code and s.NO = t.NO and s.ID = t.ID)
WHEN NOT MATCHED by target
--Newly added values in Values has been updated in tbl__QueueTable_Codes
THEN INSERT(NO, Name, ID, Code, Deleted)
VALUES(s.[NO], s.[Name], s.[ID], s.[Code], 0)
WHEN NOT MATCHED by source
--It means the value has been deleted in Value, hence we put a flag for deleted ones as '1'.
THEN UPDATE
SET t.Deleted = 1;
END
public class ReadWriteWithProgress {
public List<ClassWeAreReading> ReadData(Action<int> rowCountReporter) {
List<ClassWeAreReading> result = new List<ClassWeAreReading>();
using (SqlConnection connection = new SqlConnection("Server=localhost;Integrated Security=true;Initial Catalog=MyDatabase;"))
{
connection.Open();
var queryToExecute = "SELECT Id, Name FROM dbo.Table";
using (SqlCommand command = new SqlCommand(queryToExecute, connection))
{
using (SqlDataReader dataReader = command.ExecuteReader())
{
if (dataReader != null) {
int rowCounter = 0;
while (dataReader.Read()) {
var intermediateResult = new ClassWeAreReading();
intermediateResult.Id = (int) dataReader["Id"];
intermediateResult.Name = dataReader["Name"].ToString();
rowCounter++;
if (rowCounter % 1000 == 0) {
if (rowCountReporter != null) {
rowCountReporter (rowCounter);
}
}
result.Add(intermediateResult);
}
}
}
}
}
}
public void LoadData(List<ClassWeAreReading> dataToLoad, Action<int> rowCountReporter) {
int rowCounter = 0;
// Iterate through the list (it might be IEnumerable or any other kind of collection you can iterate through) and use the callback in the same manner as above
// ...
rowCounter++;
if (rowCounter % 1000 == 0) {
if (rowCountReporter != null) {
rowCountReporter (rowCounter);
}
}
// ...
}
}
// Then you need this kind of method to use as a callback:
private static void PrintRowCount(int rowCount){
Console.WriteLine("{0} rows transferred...", rowCount);
}
private static void PrintUpdateRowCount(int rowCount){
Console.WriteLine("{0} rows written...", rowCount);
}
// And finally you can start your stuff and pass in the method:
static void Main(string[] args)
{
var readerWithProgress = new ReadWriteWithProgress();
var result = readerWithProgress.ReadData(PrintRowCount);
readerWithProgress.LoadData(result, PrintUpdateRowCount);
}