C++ builder資料庫連線大全
1.資料庫的查詢
Query1->Close();
Query1->SQL->Clear();
Query1->SQL->Add("select soft_name,soft_vision,soft_org from soft_info where ");
Query1->SQL->Add("host_ip='192.168.1.111'");
Query1->ExecSQL();
//上面是將結果集儲存在Query1中
int q=Query1->RecordCount;
if(Query1->FindFirst()) //查詢結果集中的第一條記錄
{
a=Query1->operator []("soft_name");
b=Query1->operator []("soft_vision");
c=Query1->operator []("soft_org");
Memo1->Lines->Add(" Èí¼þÃû£º"+a);
Memo1->Lines->Add(" Èí¼þ°æ±¾£º"+b);
Memo1->Lines->Add(" Èí¼þÉÌ£º"+c);
Memo1->Lines->Add("");
int m=1;
while(m<q){
if(Query1->FindNext()) //查詢結果集中的下一條記錄
{
a=Query1->operator []("soft_name");
b=Query1->operator []("soft_vision");
c=Query1->operator []("soft_org");
Memo1->Lines->Add(" Èí¼þÃû£º"+a);
Memo1->Lines->Add(" Èí¼þ°æ±¾£º"+b);
Memo1->Lines->Add(" Èí¼þÉÌ£º"+c);
Memo1->Lines->Add("");
}
m++;
}
}
//上面是將每條記錄存到控制元件中
2.資料庫的新增
Query1->Close();
Query1->SQL->Clear();
Query1->SQL->Add("insert into soft_info (soft_name,soft_vision,soft_org,host_ip) values ('");
Query1->SQL->Add(name->operator [](j)+"','");
Query1->SQL->Add(version->operator [](j)+"','");
Query1->SQL->Add(pub->operator [](j)+"','");
Query1->SQL->Add("109.128.10."+AnsiString(j+1)+"')");
Query1->ExecSQL();
3.資料庫的刪除
Query1->Close();
Query1->SQL->Clear();
Query1->SQL->Add("delete from soft_info");
Query1->ExecSQL();
Query1->DataSource->DataSet->Refresh();