如何动态创建具有自定义日期格式的日期字段的Access数据库? [英] How to dynamically create access database with date field having custom date format?

查看:68
本文介绍了如何动态创建具有自定义日期格式的日期字段的Access数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!有人可以帮我解决当前的问题吗?

Hi! Can anyone please help me on my current problem?

我正在做一个动态创建访问数据库(.mdb文件)的项目.数据库中有一个日期字段,但我希望它具有自定义日期格式.

I am doing a project that dynamically creates access database (.mdb file). the database have a date field but I want it to have custom date format.

这是我到目前为止所拥有的.

Here is what I have so far.

Dim con As New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source =" & databaseName)
            con.Open()
            Dim dbSchema As DataTable = con.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, _
                                                                New Object() {Nothing, Nothing, tableName, "TABLE"})
            con.Close()
            If dbSchema.Rows.Count > 0 Then
'I have some code here
            Else
'This is where I create the table
                Dim cmd As New OleDb.OleDbCommand("CREATE TABLE [" + tableName + "] ([Meter] TEXT(2), [Name] TEXT(20), " _
                                                  & "[Element1] TEXT(20), [Element2] TEXT(20), [Element3] TEXT(20), [W] TEXT(20), " _
                                                  & "[pf] TEXT(20), [kWh_Delivered] TEXT(20), [kWh_Received] TEXT(20), " _
                                                  & "[Date_Time] Date(mm/dd/yyyy hh:nn:ss))", con)
                cmd.ExecuteNonQuery()
                con.Close()
                MessageBox.Show("Table Created Successfully")

            End If

谢谢!












推荐答案

似乎您认为DateField是一种字符串.

It seems that you think that a DateField is a kind of string. 

不是,它是一个Int64,其值刻度每100纳秒计数一次.

It ain't, it is a Int64 with as value ticks which count every 100th nanosecond. 

通过使用用户的标准区域性设置或使用重载的ToString格式之一,您可以获得所需的任何表示形式.

By using the standard culture settings of the user or with one of the overloaded ToString formats you can get in any representation you want. 


这篇关于如何动态创建具有自定义日期格式的日期字段的Access数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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