如何通过 vb.net 中的 SQL 向 ms-access 添加“布尔”列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13605663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a 'Boolean' column to ms-access via SQL in vb.net
提问by Pezzzz
I am trying to add a Boolean
column to a table in ms-access using SQL. I am using JET, here are the the SQL queries I have tried.
我正在尝试Boolean
使用 SQL 向 ms-access 中的表添加一列。我正在使用 JET,这是我尝试过的 SQL 查询。
Query = "ALTER TABLE tabDatafiveMinutely ADD CON0001 BOOLEAN DEFAULT FALSE"
Query = "ALTER TABLE tabDatafiveMinutely ADD CON0001 BOOLEAN"
The error I am getting is 'Syntax error in field definition'
我得到的错误是“字段定义中的语法错误”
Thanks for your help
谢谢你的帮助
EDIT:
编辑:
I would now like to make the default null
rather than false
. I have tried default null
and this still gives me false
, can anyone help with this?
我现在想设置默认值null
而不是false
. 我试过了default null
,这仍然给我false
,有人可以帮忙吗?
RESULT:
结果:
An ms-access database can only take true
and false
and not null. Therefore I have decided to use and integer
instead.
ms-access 数据库只能取true
andfalse
而不是 null。因此我决定使用 andinteger
代替。
回答by Olivier Jacot-Descombes
The equivalent SQL type of a Yes/No
column is BIT
列的等效 SQL 类型Yes/No
是BIT
ALTER TABLE tabDatafiveMinutely
ADD COLUMN CON0001 BIT DEFAULT 0 NOT NULL
Note
The DEFAULT statement can be executed only through the Jet OLE DB provider and ADO. It will return an error message if used through the Access SQL View user interface.
注意
DEFAULT 语句只能通过 Jet OLE DB 提供程序和 ADO 执行。如果通过 Access SQL View 用户界面使用,它将返回一条错误消息。
As @Pere points out, Jet Engine (Access' query engine) does not apply the DEFAULT value to existing rows. You must run an UPDATE statement after altering the table.
正如@Pere 指出的那样,Jet 引擎(Access 的查询引擎)不会将 DEFAULT 值应用于现有行。您必须在更改表后运行 UPDATE 语句。
UPDATE tabDatafiveMinutely SET CON0001 = 0 WHERE CON0001 IS NULL
回答by Will A
You should use the BIT datatype rather than BOOLEAN.
您应该使用 BIT 数据类型而不是 BOOLEAN。
回答by Mike Sherrill 'Cat Recall'
I'm not sure where you're reading the syntax from, but you need a better source.
我不确定你从哪里阅读语法,但你需要一个更好的来源。
ALTER TABLE tabDatafiveMinutely
ADD COLUMN CON0001 <datatype>
For a Boolean type, I think you'll need to pick from BIT, INTEGER, or CHAR(1). It's application-dependent. For example, legacy systems often use 't' and 'f' in a CHAR(1) column.
对于布尔类型,我认为您需要从 BIT、INTEGER 或 CHAR(1) 中进行选择。它依赖于应用程序。例如,遗留系统通常在 CHAR(1) 列中使用“t”和“f”。