----Problem 2 and solution
In addition to the content in the ISO file, each record in the imported table also needs the book type, issue number, and ID number. When inserting a new record, TPgCSV only processes the fields related to the data in the text file. Therefore, ,The content of these fields needs to be added by ourselves. Needless to say, it is natural to think of the AfterInsert event in Table. But the problem is that my Table is in the DataModule unit, and I must pass parameters to the AfterInsert event. Trouble! At the same time, such processing will cause maintenance chaos. Events that occur in a unit should be handled by functions or procedures in this unit as much as possible. So, I thought of writing a processing process in the current unit, and then assign this process to the AfterInsert event of Table when the program is running, and then disable it after the import is completed. It works, but it’s still troublesome! Since you will encounter such a problem in most cases, why not solve it at once and encapsulate this incident.
Same as question 1, here we still need an event handling process AfterInsert. Where should it be added this time? You can definitely see it at a glance:
PRocedure TPgCSV.CSVToDataSet;
begin
…
FDataset.DisableControls;
while (not Eof(FFile)) and (not FStop) do
begin
//read from CSV
Readln(FFile,RecordString);
//xm4014's modification
if Assigned(FRegulateString) then
FRegulateString(self,RecordString);
//add new record
try
FDataset.Append;
//Should be added here!
//xm4014's modification
if Assigned(FAfterInsert) then
AfterInsert(self,FDataset);
…
for i:=1 to CountMapItems do
…
end;
Similarly, new event declarations and event attributes need to be defined here, because the parameters of FDataSet need to be passed out. For the declaration code, please refer to the use of Delphi controls (2) (http://www.csdn.net/develop/read_article. asp?id=11855).
After recompiling the control, you can add code in the AfterInsert event to assign values to the book type, issue number, and ID number.
----Problem 3 and solution
The problems involving the database have now been solved. But there is still a display problem. The program requires using ProgressBar to display the import progress. In order to set the value of ProgressBar.Max, I need to know how many records there are in the ISO file before importing, that is A property similar to RecordCount. But there is no such property in TPgCSV.
Then let's add an attribute like this
//xm4014's modification
property CSVRecordCount : integer read FCSVRecordCount write FCSVRecordCount default 0;
How to assign a value to it? It is very simple. You can use ReadLn(F) to scan the ISO file, and then accumulate the number of scans.
The key is where to perform this processing. Obviously, for the same file, such work only needs to be done once. Since statistics need to be re-calculated for different files, we can scan the text file every time we set the file name attribute.
OK, find the file name attribute of TPgCSV
property CSVFile: string read FCSVFile write FCSVFile;
Make some small modifications
new property declaration
property CSVFile: string read FCSVFile write SetCSVFile;
Press Ctrl+shift+c to write the method code of SetCSVFile as follows
procedure TPgCSV.SetFCSVFile(const Value: string);
var
F1:TextFile;
iCount:integer;
begin
if FCSVFile<>Value then
begin
FCSVFile := Value;
//As soon as the file name is changed, rescan and change the value of FCSVRecordCount.
if FileExists(Value) then
begin
AssignFile(F1, Value);
Reset(F1);
iCount:=0;
while not Eof(F1) do
begin
ReadLn(F1);
Inc(iCount);
end;
FCSVRecordCount:=iCount;
end;
end;
end;
After compilation, we can safely call the CSVRecordCount property to obtain the record value before the import operation:
ProgressBar1.Min :=0;
ProgressBar1.Max:=PgCSV1.CSVRecordCount;
The above program passed debugging under Delphi 6.0/Win98
At this point, I’m basically done. In fact, I can count the changes I’ve made on my fingers. But it’s just such a small upgrade that makes me feel more comfortable to use it. I think I’ll use it again next time. When I use it, I will realize its value more and more. And after such analysis and modification, I have made a lot of progress, a few drops more than half a scoop, haha! It should be pointed out that TPgCSV is a simple control. It has neither very complicated relationships nor does it involve calling the core content of VCL, so there is no need to worry too much when modifying it. Once the source code is involved When there is a very complex hierarchical relationship , you need to think carefully about every line of code you add or modify, otherwise, it may affect the whole body, and it will be difficult to go back in the end. Therefore, the principle of use I mentioned is actually based on the control its own functions without any changes in substantive content. To achieve the state of doing whatever you want, everyone (me included) must continue to work hard! (Attachment: TPgCSV download address http://www.internet-bj.com/delphikongjian.asp?id=2 )