设置表字段的默认值 [英] Setting Default value for a field of table

查看:129
本文介绍了设置表字段的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好;

在一个程序中,我需要在运行时创建一个表并将其添加到(访问类型的)数据库中.该表具有一个名为Country的Text类型的字段.我想为此字段定义一个默认值.我在以下代码中使用了DEFAULT命令.我想知道为什么它会在运行时引发错误,说明字段定义中存在语法错误.你能帮我吗?

非常感谢您

Hello;

In a program I need to create and add a Table to a database (of Access type) at runtime. The table has a field of Text type called Country. I want to define a Default value for this field. I used DEFAULT command in the following code. I wonder why it raises an error at runtime saying that there is Syntax error in field definition. Could you please help me?

Thank you very much

using System.Data.OleDb;
...
 OleDbConnection myConnection = new OleDbConnection();
            myConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= "
                                           + @"E:\MyDataBase.accdb";
            OleDbCommand myCommand = new OleDbCommand();
            myCommand.Connection = myConnection;

            myCommand.CommandText = "CREATE TABLE Friends" +
                                      " (" +
                                        " ID int IDENTITY(1,1) PRIMARY KEY," +
                                        " FName Char(10)," +
                                        " LName Char(10)," +
                                        " Country Char(10) DEFAULT(''myCountry'')"+
                                        " )";
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();

推荐答案

尝试像这样更改查询:
Try changing your query like that:
myCommand.CommandText = "CREATE Table Friends (" +
"ID  COUNTER  PRIMARY KEY, " +
" Fname Text(10)," +
" Lname Text(10)," +
" Country Text(10) DEFAULT \"myCountry\")";


国家/地区字符(10)DEFAULT(''myCountry'')"

将"myCountry"放在方括号中.您无需将其放在方括号中.
喜欢:
Country Char(10) DEFAULT ''myCountry''

在这里查看示例:
示例-1 [示例-2 [
Country Char(10) DEFAULT(''myCountry'')"

Put ''myCountry'' outside brackets. You don''t need to put it in brackets.
Like:
Country Char(10) DEFAULT ''myCountry''

Look here for samples:
Samples - 1 [^]
Samples - 2[^]


这篇关于设置表字段的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆