这是一段Dbf 转换成sql的代码,请看看哪里可以优化,谢谢。 |
尚未結案
|
fbms
一般會員 發表:16 回覆:29 積分:14 註冊:2003-07-09 發送簡訊給我 |
[code cpp] //--------开始dbf2sql void __fastcall TForm1::start() { String fields[1000]; Memo1->Lines->Clear(); status = true; try { TStringList *list = new TStringList; // 存储需要转换的字段.memo目前不转换. list->Clear(); int count; // 字段数量 count = Dbf1->FieldCount; // ShowMessage(count); /* if(count != 0) { Memo1->Lines->AddStrings(Dbf1->FieldList); // 取得字段名字表 for(int i = 0; i < count; i ) { // ShowMessage(Dbf1->FieldList->Strings[i]); Memo1->Lines->Add(gettype(Dbf1->FieldList->Strings[i])); float length; length = Dbf1->Fields->FieldByName(Dbf1->FieldList->Strings[i])->DataSize; Memo1->Lines->Add(length); } } */ String database; try { if(ListBox1->ItemIndex > -1) { database = ListBox1->Items->Strings[ListBox1->ItemIndex]; } else { reset(); ShowMessage("必须要选择库!"); return; } } catch(...) { reset(); ShowMessage("必须要选择库!"); return; } String table; // 表名 if(Trim(Edit4->Text) == "") { if(ListBox2->ItemIndex < 0) { reset(); ShowMessage("必须要选择或者自定义要导入的表名!"); return; } else { table = ListBox2->Items->Strings[ListBox2->ItemIndex]; } } else { table = Edit4->Text; } String tablestr; // 存放insert时 values 前面的字段名 String type; if(database != "" && table != "") { ADOQuery1->Close(); ADOQuery1->SQL->Clear(); ADOQuery1->SQL->Add("use " database); ADOQuery1->SQL->Add("create table " database ".." table); ADOQuery1->SQL->Add("( "); } else { return; } if(count != 0) { // Memo1->Lines->AddStrings(Dbf1->FieldList); // 取得字段名字表 for(int i = 0; i < count; i ) { fields[i] =Dbf1->FieldDefs->Items[i]->Name; String tname; if(RadioButton1->Checked) { tname = LowerCase(Dbf1->FieldDefs->Items[i]->Name); } else { tname = Dbf1->FieldDefs->Items[i]->Name; } type = gettype(i); if(type == "unknown") { // listno->Add(i 1); ShowMessage("第" String(i 1) "个字段类型不能识别,被忽略!"); continue; } if(type == "memo") { ShowMessage("第" String(i 1) "个字段是memo字段,暂时不能支持,被忽略"); continue; } else { if(i == count - 1) { list->Add(tname " " type); list->Add( ") "); tablestr = tablestr tname; // 如果是最后一个字段 } else { list->Add(tname " " type " , "); tablestr = tablestr tname " , "; } } } ADOQuery1->SQL->AddStrings(list); Memo1->Lines->AddStrings(ADOQuery1->SQL); ADOQuery1->ExecSQL(); ADOQuery1->SQL->Clear(); if(CheckBox1->Checked == true) { ADOQuery1->SQL->Add("truncate table " table); Memo1->Lines->Add(" "); Memo1->Lines->Add("truncate table " table); Memo1->Lines->Add(" "); } String insert; // settext("insert into " table); // ShowMessage(stopi); // ShowMessage(Dbf1->RecNo); SAConnection con; // connection object SACommand cmd; // create command object con.Connect("LXZQWZH@jz30data", "sa", "sa", SA_SQLServer_Client); cmd.setConnection(&con); for(int i = stopi; i < Dbf1->RecordCount; i ) { if(!status) { stopi = i; return; } // database ".." //ShowMessage( fields[1]); insert = "insert into " database ".." table " ( " tablestr " ) values ( "; for(int j = 0; j < count; j ) { String tmptype= gettype(j); if(j == count - 1) { if(tmptype == "int") { insert = insert Dbf1->FieldByName(fields[j])->AsString " )"; } else if(gettype(j, 1) == "float") { insert = insert FloatToStr(Dbf1->FieldByName(fields[j])->AsFloat) " )"; } else if(tmptype == "memo") { // 忽略的字段类型 } else { insert = insert " \'" Dbf1->FieldByName(fields[j])->AsString "\' )"; } } else { if(tmptype == "int") { insert = insert Dbf1->FieldByName(fields[j])->AsString " , "; } else if(gettype(j, 1) == "float") { insert = insert FloatToStr(Dbf1->FieldByName(fields[j])->AsFloat) " , "; } else if(tmptype == "memo") { // 忽略的字段类型 } else { //insert = insert " \'" Dbf1->FieldByName(Dbf1->FieldDefs->Items[j]->Name)->AsString "\', "; insert = insert " \'" Dbf1->FieldByName(fields[j])->AsString "\', "; } } } Dbf1->Next(); // ADOQuery1->SQL->Add(insert); // Memo1->Lines->Add(insert); try { cmd.setCommandText(insert.c_str()); cmd.Execute(); } catch(...) { } // //StatusBar1->Panels->Items[1]->Text = "当前记录数:" IntToStr(Dbf1->RecNo); RzProgressBar1->Percent = (float)i / Dbf1->RecordCount * 99; // if ((int)Dbf1->RecordCount/(i 1)/100==2) // { Application->ProcessMessages(); //} // Memo1->Lines->Add(insert); } // ADOQuery1->ExecSQL(); // ADOQuery1->SQL->Clear(); // commit changes on success try { con.Commit(); } catch(SAException & x) { // 异常处理 try { // 退出当前事务 // con.Rollback(); } catch(SAException &) { } // 显示错误信息 ShowMessage((const char *)x.ErrText()); } ListBox1->Enabled = true; Memo1->Lines->Add("--insert finished," String(Dbf1->RecNo) " record(s) inserted!"); RzProgressBar1->Percent = 100; delete list; reset(); } } catch(Exception & exception) { Application->ShowException(&exception); Memo1->Lines->Add("--error ,stop!" IntToStr(Dbf1->RecNo)); // Button2->Click(); reset(); } catch(...) { reset(); } } [/code] 我感觉还是不够快,我的代码够烂。请各位高手给看看,谢谢。 控件用的Tdbf,还有Ado,等。 |
fbms
一般會員 發表:16 回覆:29 積分:14 註冊:2003-07-09 發送簡訊給我 |
con.Connect("mailto:LXZQWZH@jz30data">LXZQWZH@jz30data", "sa", "sa", SA_SQLServer_Client);
这一句话应该是: con.Connect("LXZQWZH@jz30data", "sa", "sa", SA_SQLServer_Client); cmd.setConnection(&con); 是sqlapi lib 的连接语句。 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |