Delphi 7:ADO,需要基本的编码示例 [英] Delphi 7: ADO, need basic coding example
问题描述
- 创建一个数据库
- 添加一个简单的表
-
- 关闭数据库
然后,稍后
- 打开数据库
- 阅读每个表
- 读取给定表格的每个字段
- 执行一个简单的搜索
对不起,这么无聊。我做了google,但没有找到有用的教程...
另外,如果底层数据库是MySql(5.1.36)(I甚至不知道是否有任何差异)
@mawg,我为您写了一个简单的程序来阻止工作如何与ADO和德尔福。这是一个控制台应用程序,但解释了基础。
在执行此代码之前,您必须从此位置。
您可以改进和调整此代码
程序ProjectMysqlADO;
{$ APPTYPE CONSOLE}
使用
ActiveX,
DB,
ADODB,
SysUtils;
const
//连接字符串
StrConnection ='Driver = {MySQL ODBC 3.51 Driver}; Server =%s; Database =%s; User =%s; Password =%s; Option = 3;';
var
AdoConnection:TADOConnection;
程序SetupConnection(DataBase:String); //打开连接
begin
Writeln('Connecting to MySQL');
AdoConnection:= TADOConnection.Create(nil);
AdoConnection.LoginPrompt:= False; //不要求登录参数
AdoConnection.ConnectionString:=格式(StrConnection,['your_server',DataBase,'your_user','your_password']);
AdoConnection.Connected:= True; //打开连接
Writeln('Connected');
结束
程序CloseConnection; //关闭一个打开的连接
begin
Writeln('关闭连接到MySQL');
如果AdoConnection.Connected然后
AdoConnection.Close;
AdoConnection.Free;
Writeln('Connection closed');
结束
程序CreateDatabase(Database:string);
begin
Writeln('创建数据库'+数据库);
AdoConnection.Execute('CREATE DATABASE IF NOT EXISTS'+ Database,cmdText);
Writeln('Database'+ database +'created');
结束
procedure CreateTables;
begin
Writeln('Creating Tables');
AdoConnection.Execute(
'CREATE TABLE IF NOT EXISTS customers('+
'id INT,'+
'name VARCHAR(100),'+
'国家VARCHAR(25))',cmdText);
Writeln('Tables Created');
结束
程序DeleteData;
begin
Writeln('删除虚拟数据');
AdoConnection.Execute('DELETE FROM customers');
Writeln('Data deleted');
结束
procedure InsertData;
过程InsertReg(id:integer; name,country:string);
var
ADOCommand:TADOCommand;
begin
ADOCommand:= TADOCommand.Create(nil);
try
ADOCommand.Connection:= AdoConnection;
ADOCommand.Parameters.Clear;
ADOCommand.CommandText:='INSERT INTO customers(id,name,country)VALUES(:id,:name,:country)';
ADOCommand.ParamCheck:= False;
ADOCommand.Parameters.ParamByName('id')。Value:= id;
ADOCommand.Parameters.ParamByName('name')。Value:= name;
ADOCommand.Parameters.ParamByName('country')。Value:= country;
ADOCommand.Execute;
finally
ADOCommand.Free;
结束
结束
begin
Writeln('插入数据');
InsertReg(1,'Lilian Kelly','UK');
InsertReg(2,'John and Sons','USA');
InsertReg(3,'William Suo','USA');
InsertReg(4,'MARCOTEC','UK');
Writeln('Data Inserted');
结束
procedure ReadData;
var
AdoQuery:TADOQuery;
begin
AdoQuery:= TADOQuery.Create(nil);
try
AdoQuery.Connection:= AdoConnection;
AdoQuery.SQL.Add('SELECT * FROM customers');
AdoQuery.Open;
而不是AdoQuery.eof do
begin
Writeln(format('%s%s%s',[AdoQuery.FieldByname('id')。AsString,AdoQuery.FieldByname('name ').AsString,AdoQuery.FieldByname('country')。AsString]));
AdoQuery.Next;
结束
finally
AdoQuery.Free;
结束
结束
begin
CoInitialize(nil); //调用CoInitialize()
try
Writeln('Init');
try
SetupConnection('mysql'); //首先连接到mysql数据库,这个数据库总是存在
CreateDatabase('Mydb'); //现在我们创建数据库
CloseConnection; //关闭原来的连接
SetupConnection('Mydb'); //打开指向Mydb数据库的连接
CreateTables; //创建样本表
DeleteData; //删除伪数据插入
InsertData; //插入一个虚拟数据
ReadData; //读取插入的数据
CloseConnection; //关闭连接
除了
在E:Exception do
Writeln(E.Classname,':',E.Message);
结束
Readln;
finally
CoUnInitialize; // free memory
end;
结束。
I am a complete beginner here. Can someone please post some Delphi code to
- create a database
- add a simple table
- close the database
then, later
- open a database
- read each table
- read each field of a given table
- perform a simple search
Sorry to be so clueless. I did google, but didn't find a useful tutorial ...
In addition, it would be useful if the underlying database were MySql (5.1.36) (I don't even know if that makes any difference)
@mawg, i wrote an simple program for you to ilustrate how work with ADO and Delphi. this is an console application, but explains the basics.
before you execute this code you must download and install the odbc connector from this location.
You can improve and adapt this code to your requirements.
program ProjectMysqlADO;
{$APPTYPE CONSOLE}
uses
ActiveX,
DB,
ADODB,
SysUtils;
const
//the connection string
StrConnection='Driver={MySQL ODBC 3.51 Driver};Server=%s;Database=%s;User=%s; Password=%s;Option=3;';
var
AdoConnection : TADOConnection;
procedure SetupConnection(DataBase:String);//Open a connection
begin
Writeln('Connecting to MySQL');
AdoConnection:=TADOConnection.Create(nil);
AdoConnection.LoginPrompt:=False;//dont ask for the login parameters
AdoConnection.ConnectionString:=Format(StrConnection,['your_server',DataBase,'your_user','your_password']);
AdoConnection.Connected:=True; //open the connection
Writeln('Connected');
end;
procedure CloseConnection;//Close an open connection
begin
Writeln('Closing connection to MySQL');
if AdoConnection.Connected then
AdoConnection.Close;
AdoConnection.Free;
Writeln('Connection closed');
end;
procedure CreateDatabase(Database:string);
begin
Writeln('Creating Database '+database);
AdoConnection.Execute('CREATE DATABASE IF NOT EXISTS '+Database,cmdText);
Writeln('Database '+database+' created');
end;
procedure CreateTables;
begin
Writeln('Creating Tables');
AdoConnection.Execute(
'CREATE TABLE IF NOT EXISTS customers ('+
'id INT,'+
'name VARCHAR(100),'+
'country VARCHAR(25) )',cmdText);
Writeln('Tables Created');
end;
procedure DeleteData;
begin
Writeln('Deleting dummy data');
AdoConnection.Execute('DELETE FROM customers');
Writeln('Data deleted');
end;
procedure InsertData;
Procedure InsertReg(id:integer;name,country:string);
var
ADOCommand : TADOCommand;
begin
ADOCommand:=TADOCommand.Create(nil);
try
ADOCommand.Connection:=AdoConnection;
ADOCommand.Parameters.Clear;
ADOCommand.CommandText:='INSERT INTO customers (id,name,country) VALUES (:id,:name,:country)';
ADOCommand.ParamCheck:=False;
ADOCommand.Parameters.ParamByName('id').Value := id;
ADOCommand.Parameters.ParamByName('name').Value := name;
ADOCommand.Parameters.ParamByName('country').Value := country;
ADOCommand.Execute;
finally
ADOCommand.Free;
end;
end;
begin
Writeln('Inserting Data');
InsertReg(1,'Lilian Kelly','UK');
InsertReg(2,'John and Sons','USA');
InsertReg(3,'William Suo','USA');
InsertReg(4,'MARCOTEC','UK');
Writeln('Data Inserted');
end;
procedure ReadData;
var
AdoQuery : TADOQuery;
begin
AdoQuery:=TADOQuery.Create(nil);
try
AdoQuery.Connection:=AdoConnection;
AdoQuery.SQL.Add('SELECT * FROM customers');
AdoQuery.Open;
while not AdoQuery.eof do
begin
Writeln(format('%s %s %s',[AdoQuery.FieldByname('id').AsString,AdoQuery.FieldByname('name').AsString,AdoQuery.FieldByname('country').AsString]));
AdoQuery.Next;
end;
finally
AdoQuery.Free;
end;
end;
begin
CoInitialize(nil); // call CoInitialize()
try
Writeln('Init');
try
SetupConnection('mysql'); //first will connect to the mysql database , this database always exist
CreateDatabase('Mydb'); //now we create the database
CloseConnection; //close the original connection
SetupConnection('Mydb'); //open the connection pointing to the Mydb database
CreateTables; //create a sample table
DeleteData; //Delete the dummy data before insert
InsertData; //insert a dummy data
ReadData; //read the inserted data
CloseConnection; //close the connection
except
on E : Exception do
Writeln(E.Classname, ': ', E.Message);
end;
Readln;
finally
CoUnInitialize; // free memory
end;
end.
这篇关于Delphi 7:ADO,需要基本的编码示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!