C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[19939] [질문]DBGrid의 내용을 CSV File로 저장시 속도문제 및 File분할문제.
김재영 [] 1573 읽음    2002-07-09 04:01
C++ Builder 초보입니다.
현재 개발하고 있는 애플리케이션내에서 화면으로 조회하는 모든 데이타는 CSV File로 저장할
수 있습니다.

그런데...데이타 건수가 많아지면 Performance에 문제가 생기며,
또하나 새로운 문제는 엑셀에서는 데이타가 4-5만건(?)이상일 때는 불러들일 수 없는 줄로 알고
있습니다.

현재 데이타가 만건이 넘으면 Application이 응답이 없는 중대한 문제가 있구요.

고수님들의 노하우를 듣고 싶습니다.
첫째, 데이타를 쪼개어서 각기 연속된 파일로 저장하는 방법과
둘째, DBGrid의 각 셀마다 구분자','를 주게되는데, 이로인해 발생되는 Performance문제...를
      해결할 수 있는가...

고수님들은 이런 경험이 없으신지요?
여기저기 이에 관한 정보를 찾고자 검색을 했지만 찾을 수가 없습니다.

도와주시겠습니까?

참고로...제 Source를 보여드리겠습니다.

void __fastcall TMainForm::CommaSeparatedFile()
{
        /*
                Helper function to parse the current form's data into
                a CSV format file
        */
        TForm* TheForm=ActiveMDIChild;
        if (!TheForm) return;

        SaveDialog->FileName=TheForm->Caption;
        SaveDialog->Filter = "CSV File (*.csv)|*.csv";

        String fName = SaveDialog->FileName;
        fName += ".csv";
        bool bMore=true;
        while (bMore)
        {
                if (!SaveDialog->Execute()) return;
                if (FileExists(fName))
                {
                        int nResponse=MessageDlg("Overwrite the Existing file?", mtWarning, TMsgDlgButtons() << mbYes << mbNo << mbCancel, 0);
                        if (mrYes==nResponse)
                                bMore=false;
                        else if (mrCancel==nResponse)
                                return;
                } else bMore=false;
        }

        TQuery *pqDisplay=GetQueryForTheForm(TheForm);

        TDBGrid *pdbgDisplay=NULL;

        for (int i=0; pdbgDisplay==NULL && i<TheForm->ControlCount; i++)
                if (TheForm->Controls[i]->Name=="dbgDisplay")
                        pdbgDisplay=(TDBGrid*) TheForm->Controls[i];

        if (pdbgDisplay==NULL || pqDisplay==NULL)
                return;

        int nFileHandle=FileCreate(fName);
        if (nFileHandle==-1) return;

        TCursor oldCursor=Screen->Cursor;
        Screen->Cursor=crHourGlass;    // Show hourglass cursor

        String str1="";
        for (int i=0; i<pqDisplay->FieldCount-1; i++)
                str1+=pdbgDisplay->Columns[0].Items[i]->Title->Caption + ","; 

        str1+="\r\n";
        // Write out the number of rows and columns in the grid.
        FileWrite(nFileHandle, (char*) str1.c_str(), str1.Length());

        DWORD dw1=GetTickCount();
        TWindowState wsOldWindowState=TheForm->WindowState;

        //This fixes the RecordCount = -1 problem
        String sSQL = pqDisplay->SQL->Text;
        sSQL = strstr(sSQL.c_str(), " FROM ");
        if (sSQL == NULL)
                sSQL = strstr(sSQL.c_str(), " from ");
        sSQL = "select COUNT(*) as COUNT " + sSQL;
        qUpdate->SQL->SetText(sSQL.c_str());
        qUpdate->Open();

        String strCount=
                qUpdate->FieldByName("COUNT")->AsString;
        int currCount = StrToInt(strCount);

        //if (pqDisplay->RecordCount>100)
        if (currCount>100)
        {
                TheForm->WindowState=wsMinimized;
                pdbgDisplay->Visible=false;
        }
        pqDisplay->First();
        //This fixes the RecordCount = -1 problem
        //for (int i=0; i<pqDisplay->RecordCount; i++)
        int i=0;
        while (!pqDisplay->Eof)
        {
                str1="";
                String str2="";
                for(int j = 1+1; j < pqDisplay->FieldCount; j++)   
                {
                        str2=pqDisplay->Fields->Fields[j]->AsString;
                       
                        if (0!=str2.Pos(",") && 0!=str2.Pos("\""))
                        {
                                String str3="";
                                for (int i=1; i<str2.Length(); i++)
                                {
                                        if (str2[i]=='\"')
                                                str3+='\"';
                                        str3+=str2[i];
                                }
                                str2=str3;
                        }
                        if (0!=str2.Pos(","))
                                str1+="\"";
                        str1+=str2;
                        if (0!=str2.Pos(","))
                                str1+="\"";
                        str1+=",";
                }

                str1+="\r\n";

                str2="";
                str2.sprintf("Processing: %d/%d", i+1, currCount);
                StatusLine->Panels->Items[0]->Text =str2;

                FileWrite(nFileHandle,
                        (char*) str1.c_str(),
                        str1.Length());

                // Write out the number of rows and columns in the grid.
                pqDisplay->Next();
                i++;
        }

        pqDisplay->First();

        StatusLine->Panels->Items[0]->Text =GetTickCount()-dw1;
        pdbgDisplay->Visible=true;
        TheForm->WindowState=wsOldWindowState;

        FileClose(nFileHandle);

        Screen->Cursor=oldCursor; // always restore the cursor

        MessageDlg("Done, Results in the File: " + fName, mtInformation, TMsgDlgButtons() << mbOK, 0);
}

+ -

관련 글 리스트
19939 [질문]DBGrid의 내용을 CSV File로 저장시 속도문제 및 File분할문제. 김재영 1573 2002/07/09
19948     Re:[질문]DBGrid의 내용을 CSV File로 저장시 속도문제 및 File분할문제. 김백일 2135 2002/07/09
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.