C++ builder 2007 使用ADO数据库 进行 添加 删除 修改
时间:2009-04-30 13:55来源:本站原创 作者:陈华林 点击:112次
ado.rar 打包下载
ado.cpp
//---------------------------------------------------------------------------
#pragma hdrstop
#include "ado.h"
#include <vcl.h>
#include <ADODB.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
/*
bool insertData(char *sql);
bool sqldata(char* sql);
bool deldata(char*sql);
bool updata(char*sql);
*/
bool insertData(char* sql)
{
AnsiString connStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d://data.mdb;User Id=admin;Password=;";
TADOConnection *conn = new TADOConnection(NULL);
conn->LoginPrompt= false; //去掉登录对话框
conn->ConnectionString=connStr;
conn->Open();
TADOCommand *cmd=new TADOCommand(NULL);
cmd->Connection=conn;
//添加
AnsiString cmdStr_add=sql;//"INSERT INTO [work] (yonghu,mima) VALUES ('Wilson','123456')"; //work 为关键字,搞死我了 FUCK
cmd->CommandText=cmdStr_add;
cmd->Execute();
delete cmd;
conn->Close();
delete conn;
return true ;
}
bool deldata(char* sql)
{
AnsiString connStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D://data.mdb;User Id=admin;Password=;";
TADOConnection *conn = new TADOConnection(NULL);
conn->LoginPrompt= false; //去掉登录对话框
conn->ConnectionString=connStr;
conn->Open();
TADOCommand *cmd=new TADOCommand(NULL);
cmd->Connection=conn;
//删除
AnsiString cmdStr_del=sql;//"delete * from [work] where id=2"; //work 为关键字,搞死我了 FUCK
cmd->CommandText=cmdStr_del;
cmd->Execute();
delete cmd;
conn->Close();
delete conn;
return true ;
}
bool updata(char* sql)
{
AnsiString connStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D://data.mdb;User Id=admin;Password=;";
TADOConnection *conn = new TADOConnection(NULL);
conn->LoginPrompt= false; //去掉登录对话框
conn->ConnectionString=connStr;
conn->Open();
TADOCommand *cmd=new TADOCommand(NULL);
cmd->Connection=conn;
//更新
AnsiString cmdStr_update=sql;//"UPDATE [work] set yonghu = '陈华林',mima = '123456789' where id=5 "; //work 为关键字,搞死我了 FUCK
cmd->CommandText=cmdStr_update;
cmd->Execute();
delete cmd;
conn->Close();
delete conn;
return true ;
}
bool sqldata(char* sql)
{
AnsiString connStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D://data.mdb;User Id=admin;Password=;";
TADOConnection *conn = new TADOConnection(NULL);
conn->LoginPrompt= false; //去掉登录对话框
conn->ConnectionString=connStr;
conn->Open();
//读取
TADODataSet *ds=new TADODataSet(NULL);
ds->Connection=conn;
AnsiString cmdStr_sql=sql;//"select * from [work]";
ds->CommandText=cmdStr_sql;
ds->Open();
AnsiString rs=ds->FieldByName("yonghu")->AsWideString;
ShowMessage(strcat("取到的数据 yonghu=",rs.c_str()));
ds->Close();
delete ds;
conn->Close();
delete conn;
return true ;
}
ado.h
//---------------------------------------------------------------------------
#ifndef adoH
#define adoH
//---------------------------------------------------------------------------
#endif
bool openConnect();
bool insertData(char* sql);
bool sqldata(char* sql);
bool deldata(char* sql);
bool updata(char* sql);