什么办法都想尽了,谁会使用ADOX的高手请进入(也许李维能解)(300分)

D

dxpjj

Unregistered / Unconfirmed
GUEST, unregistred user!
有谁会使用ADOX对Access创建数据库和表,特别是创建自动编号的字段,允许空字符串的字段
我不想用DAO,在ADO2.1的帮助中有VB的代码,可在Delphi中实现不了,按照李维的
《Delphl5.x ADO/MTS/CoM+高级程序设计篇》中讲的也不能实现创建自动编号和允许空
字符串的字段,[:(]若高人指导,定给300大洋(给代码)
 
高手们都吃饭去了
 
用sql语句,参见sqlserver的帮助文件,里面讲的很多,但注意有些不适用于access
 
字段类型为autoincrement可以创建自动编号的字段。
Create Table Type2(
ID autoincrement,
ParentID string, '允许空
Name VarChar(32) NOT NULL,
Constraint Type_PK PRIMARY kEY(ID)
)
 
将上面的放到TADOCommand中,运行后生成Type2表,别忘了将 '允许空  去掉。
 
我按照飘摇客的方法能够创建自动编号的字段,Name VarChar(32) NOT NULL是创建
是否必填字段而不是允许空字符串,请看一下Access中有两个属性,必填字段和允许
空字符串是两个不同的概念。
 
with ADOQuery1 do
begin
close;
sql.clear;
sql.add('create table Type2(');
sql.add('ID int identity,');
sql.add('ParentID string,');
sql.add('Name VarChar(32) NOT NULL,');
sql.add('primary key (ID))');
execsql;
end;
 
to cmz
你的方法跟飘摇客一样,不能生成允许空字符串的字段
 
对于Access来说,使用ADOX应该是最明智的了

给你一段我的代码,注意加上uses Activex, Comobj, AdoInt等
procedure NewColumn(var AColumns: OleVariant)
//AColumns是某Table的Columns对象
const
NULL_ATTRIBUTES: array[boolean] of integer = ($00000001, $00000002);
var
AColumn: OleVariant;
begin
AColumn := CreateOleObject('ADOX.Column');
AColumn.Name := 'Field1';
AColumn.Type := adVarWChar;
AColumn.DefinedSize := 100;
AColumn.Attributes := NULL_ATTRIBUTES[True]
//是否允许null
AColumns.Append(AColumn)
//添加
AColumn.Properties['Description'].Value := 'Description';
AColumn.Properties['Jet OLEDB:Allow Zero Length'].Value := True
//是否允许空字符串
end;
 
请问Adnil高手:自动编号如何现实,
 
这个也简单阿

AColumn.Type := adInteger;
...
AColumns.Append(AColumn)
//添加
AColumn.Properties['Autoincrement'].Value := True;
....
 
在delphi中Import type library,生成Adox_tlb.pas,引用它

procedure TForm1.Button1Click(Sender: TObject);
var cat:_Catalog;
tb1:_Table;
col1:_Column;
i:integer;
begin
cat:=coCatalog.Create;
cat.Set_ActiveConnection(self.ADOConnection1.ConnectionObject);
tb1:=coTable.create;
tb1.Name:='test';
cat.Tables.Append(tb1);
col1:=coColumn.Create;
col1.ParentCatalog:=cat;
col1.Name:='t2';
col1.Type_:= adinteger;

for i:=0 to col1.Properties.Count-1 do
begin
if Lowercase(col1.Properties.Item.Name) = 'autoincrement' then
col1.Properties.Item.Value:=true;
end;
tb1.Columns.Append(col1,col1.type_,col1.definedsize);


end;
 
我按zm30的方法成功了,但如何实现允许空字符串的字段呢?
 
我的方法不能实现自动增长的字段吗? 你看看允许空的属性是不是“是”。
 
//加一个"NULL",如果还不行我就要怀疑你的Access和Delphi有问题了
//加"NULL"的地方用/////////////标记
with ADOQuery1 do
begin
close;
sql.clear;
sql.add('create table Type2(');
sql.add('ID int identity,');
sql.add('ParentID string NULL,');////////////////////
sql.add('Name VarChar(32) NOT NULL,');
sql.add('primary key (ID))');
execsql;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var cat:_Catalog;
tb1:_Table;
col1:_Column;
i:integer;
begin
cat:=coCatalog.Create;
cat.Set_ActiveConnection(self.ADOConnection1.ConnectionObject);
tb1:=coTable.create;
tb1.Name:='test';
cat.Tables.Append(tb1);
col1:=coColumn.Create;
col1.ParentCatalog:=cat;
col1.Name:='t2';
col1.Type_:= adinteger;

for i:=0 to col1.Properties.Count-1 do
begin
if Lowercase(col1.Properties.Item.Name) = 'autoincrement' then
col1.Properties.Item.Value:=true;
if Lowercase(col1.Properties.Item.Name) = 'jet oledb:allow zero length' then
col1.Properties.Item.Value:=true;
end;
tb1.Columns.Append(col1,col1.type_,col1.definedsize);

col1._Release;

col1:=coColumn.Create;
col1.ParentCatalog:=cat;
col1.Name:='t3';
col1.Type_:= adVarWChar;
col1.DefinedSize:=10;

for i:=0 to col1.Properties.Count-1 do
begin

if Lowercase(col1.Properties.Item.Name) = 'jet oledb:allow zero length' then
col1.Properties.Item.Value:=false
//这里把允许空字符串设为false,因为默认为true
end;
tb1.Columns.Append(col1,col1.type_,col1.definedsize);

end;
其实你可以在for循环里查看Column有哪些属性,里面一定有你想要的,自己多动动手
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
555
import
I
I
回复
0
查看
579
import
I
顶部