在 ACCESS 中添加一个具有默认值的字段

发布于 2024-07-10 12:41:13 字数 418 浏览 7 评论 0原文

我想使用 Visual C++ 代码插入一个具有默认值的新字段。 我写过这个:

CADODatabase pDB;
String strConnessione = _T("Provider=Microsoft.Jet.OLEDB.4.0;""Data Source=");
strConnessione = strConnessione + "MioDatabase.mdb";
pDB.SetConnectionString(strConnessione);
pDB.Open();

query.Format("ALTER TABLE TBProva ADD Fattore Double Default 0;");
pDB.Execute(query);

但它不正确。 我该怎么做才能做到这一点? 你们中有人可以给我写正确的代码吗?

I'd like to insert a new field with a Default value using Visual C++ Code.
I have wrote this:

CADODatabase pDB;
String strConnessione = _T("Provider=Microsoft.Jet.OLEDB.4.0;""Data Source=");
strConnessione = strConnessione + "MioDatabase.mdb";
pDB.SetConnectionString(strConnessione);
pDB.Open();

query.Format("ALTER TABLE TBProva ADD Fattore Double Default 0;");
pDB.Execute(query);

but it isn't correct. How can I do for doing it?
Someone of you Can write me the just code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇妻 2024-07-17 12:41:13

在 JET-SQL 语言中,您必须使语法更加具体,并在“ALTER TABLE”句子中添加“COLUMN”单词。 示例:

strSql = "ALTER TABLE MyTable ADD COLUMN MyField DECIMAL (28,3);"
strSql = "ALTER TABLE MyTable ADD COLUMN MyText TEXT(3);"

根据帮助,您可以定义默认值,但我从未尝试过。 此语法仅对 Access/JET 数据库有效。 我认为如果您通过 ODBC 或 ADO 访问访问表,它不会起作用。

在这种情况下,您可以通过其他方式更改数据库结构。 您会找到一个示例 在这里。 这是使用 DAO 对象模型创建的,但可以轻松切换到通用 ADO 对象模型以与 ADODB 连接一起使用。

编辑:一种“隐式 ADO 解决方案”(意味着使用 ADODB 连接但不使用 ADO 表对象等)可以如下所示:(

有关连接字符串示例,请检查 此处

Dim myConnection AS ADODB.connection
set myConnection = New ADODB.connectionString
myConnection.connectionString = 'here is your connection string'
myConnection.open
myConnection.execute "ALTER TABLE myTable ADD Column MyField DECIMAL (12,3);"
myConnection.close

如果您正在使用活动 mdb 文件,只需编写:

CurrentProject.Connection.Execute "ALTER TABLE myTable ADD Column MyField DOUBLE;"

就是这样。

如果您更喜欢使用 ADO 对象,请将 ADOX 库添加到您的代码源中并使用 Table.columns.append 方法。您将找到一个示例 此处

In JET-SQL language you have to be more specific with the syntax and add the 'COLUMN' word in the 'ALTER TABLE' sentence. Exemple:

strSql = "ALTER TABLE MyTable ADD COLUMN MyField DECIMAL (28,3);"
strSql = "ALTER TABLE MyTable ADD COLUMN MyText TEXT(3);"

According to the Help, you can define a default value but I never tried it. This syntax is only valid with Access/JET database. I do not think it will work if you reach your access table through ODBC or ADO.

In such situations, you can change your database structure through other means. You'll find an example here. This was made with DAO object model but can be easily switched to common ADO object model to be used with an ADODB connection.

EDIT: one "implicit ADO solution" (meaning using the ADODB connection but not the ADO tables objects etc" can then be the following:

(for connection string examples, check here)

Dim myConnection AS ADODB.connection
set myConnection = New ADODB.connectionString
myConnection.connectionString = 'here is your connection string'
myConnection.open
myConnection.execute "ALTER TABLE myTable ADD Column MyField DECIMAL (12,3);"
myConnection.close

If you are working with the active mdb file, just write:

CurrentProject.Connection.Execute "ALTER TABLE myTable ADD Column MyField DOUBLE;"

That's it.

If you prefer to use ADO objects, please add ADOX library to your code source and use the Table.columns.append method. You'll find an example here

鹿港巷口少年归 2024-07-17 12:41:13

您缺少 COLUMN 关键字,例如

ALTER TABLE TBProva ADD COLUMN Fattore Double Default 0;

实际上,我认为您可能也缺少 NOT NULL 部分,例如

ALTER TABLE TBProva ADD COLUMN Fattore DOUBLE DEFAULT 0 NOT NULL;

我这样说是因为 Jet 不支持使用 DEFAULT 关键字,因此只有当列声明为 NOT NULL 并且列名被省略时,才会应用 DEFAULT INSERT 中的列列表。 如果该列可以为 NULL,那么如何让引擎应用默认值?!

You are missing the COLUMN keyword e.g.

ALTER TABLE TBProva ADD COLUMN Fattore Double Default 0;

Actually, I think you might be missing the NOT NULL part too e.g.

ALTER TABLE TBProva ADD COLUMN Fattore DOUBLE DEFAULT 0 NOT NULL;

I say this because Jet doesn't support the use of the DEFAULT keyword in SQL DML, therefore the only time a DEFAULT will be applied is when the column is declared as NOT NULL and the column name is omitted from column list in an INSERT. If the column were NULLable, how would you get the engine to apply the default?!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文