如何在访问db中将其他语言日期转换为vb中的美国格式日期 [英] How to convert other language date to US format date in vb in access db

查看:101
本文介绍了如何在访问db中将其他语言日期转换为vb中的美国格式日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我们开发Web应用程序,目前还需要支持其他语言日期类型。

当我们在访问数据库的日期/时间列中保存日期时,我们转换为美国格式并保存它,但从访问数据库中检索日期值,它以其他语言显示(2558/08/14泰语格式,在泰国操作系统中运行的bcz Web应用程序)但复制/粘贴访问数据库在英文版操作系统和开放访问数据库,日期in(14/8/2015 US format)

那么如何将泰语日期从数据表转换为美国格式。



以下是我试过的





  Dim  iString 作为 字符串 = row( 0 )。ToString < span class =code-comment>' 此处row(0)有2558/08/14  
Dim oDate As DateTime = DateTime.ParseExact(iString, MM / dd / yyyy,System.Globalization.CultureInfo.InvariantCulture)





显示String未被识别为有效的DateTime。错误



注意:在泰国操作系统中运行的Web应用程序,当在访问数据库中保存日期时,我们使用以下代码转换为美国格式

 DateTime.Now.ToString(  dd-MM-yyyy,System。 Globalization.CultureInfo.InvariantCulture)











请查看泰国操作系统截图,该操作系统在泰国操作系统中安装Visual Studio

http://prntscr.com/ 85jofq [ ^ ]

http://prntscr.com/85jodq [ ^ ]





我使用VS2010在泰国操作系统内创建新的Web应用程序和Windows应用程序/>
这是web应用程序asp按钮点击事件



 受保护的  Sub  Button1_Click(发件人 As  对象,e  As  System.EventArgs)句柄 Button1.Click 
Dim cn As OleDbConnection
Dim cmd As OleDbDataAdapter
Dim dt 作为 DataTable
Dim strcmd 作为 字符串 =
cn = OleDbConnection( 提供商= Microsoft.Jet.OLEDB.4.0;数据源= C:\Program Files(x86)\OCR \UDatabase \ MM.mdb; Jet OLEDB:数据库密码=密码;
strcmd = SELECT格式(ProcessDate, yyyy / MM / dd )as PDate FROM tblFile
cmd = OleDbDataAdapter( OleDbCommand(strcmd))
cmd.SelectCommand.Connection = cn
cn.Open()
cmd.Fill(dt)
Label1.Text = dt.Rows( 0 )( ProcessDate
cn.Close()
结束 Sub





< br $>


我的网页配置是

 <?  xml     version   =  1.0  >  
< 配置 >
< system.web >
< 编译 debug = true targetFramework = 4.0 / >
< / system .web >
< / configuration >





在Windows应用程序中按钮点击事件是



 私人  Sub  Button1_Click(发件人作为系统。对象,e  As  System.EventArgs)句柄 Button1.Click 
Dim cn As OleDbConnection
Dim cmd As OleDbDataAdapter
Dim dt As DataTable
Dim strcmd 作为 字符串 =
cn = OleDbConnection( Provider = Microsoft.Jet.OLEDB.4.0;数据源= C:\Program Files(x86)\OCR \UDatabase \ MM.mdb; Jet OLEDB:数据库密码=密码;
strcmd = SELECT格式(ProcessDate, yyyy / MM / dd )as PDate FROM tblFile
cmd = OleDbDataAdapter( OleDbCommand(strcmd))
cmd.SelectCommand.Connection = cn
cn.Open()
cmd.Fill(dt)
Label1.Text = dt.Rows( 0 )( ProcessDate
cn.Close()

结束 Sub





in Access db在tblFile表中获得此值

 ID ProcessDate 
200 14/8/2015 4:41:51 PM





在Windows应用程序中同样美国格式日期显示,但在网络应用程序中显示泰国日期格式。



注意:我在使用VS2010的泰国操作系统中运行,需要在网络上指定和日期文化config?



请查看此链接以获取exe和Web应用程序结果

https://www.dropbox.com/s/3b3ano2xnqph0ls/thaios.wmv?dl=0 [ ^ ]

解决方案

< blockquote>你最好在数据库中安装UTC时间,使用 DateTime .ToUniversalTime方法 [ ^ ]将本地日期时间转换为UTC。当客户端应用程序需要显示日期时间时,您可以使用 DateTime.ToLocalTime方法 [ ^ ]


如果我正确了解情况,数据库中的基础类型是正确的日期/时间,但在插入和更新您提供的值作为字符串文字。



如果是这种情况,您应该使用参数,因为它们可以帮助您克服此类转换问题。使用参数,您可以使用在客户端应用的区域设置来设置参数值,并在添加到数据库时正确转换它们。



根据您使用的技术参数类可能不同,但如果使用OLE DB操作,请查看 OleDbParameter [ ^ ]


在MS Access数据库中存储日期数据类型的最佳方法是使用ISO格式 [ ^ ]。



如需了解更多信息,请参阅:如何存储,计算和比较日期/ Ti我在Microsoft Access中的数据 [ ^ ]

Hi we develop web application,currently need to support other language date type also.
When we save date in Date/Time column in access db we convert to US format and save it,but retrieve date value from access db,it show in other language(2558/08/14 Thai format,bcz web application running in Thailand OS) but copy/paste that access db in English version OS and open access db,the date in (14/8/2015 US format)
So how to convert Thai date to US format from datatable.

Below is i tried


Dim iString As String = row(0).ToString 'here row(0) have 2558/08/14
Dim oDate As DateTime = DateTime.ParseExact(iString, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)



It show "String was not recognized as a valid DateTime." error

Note:Web application running in Thai OS,when save date in access db we convert to US format using following code

DateTime.Now.ToString("dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture)






Pls check screenshot taken form Thai OS,which install visual studio in Thai OS
http://prntscr.com/85jofq[^]
http://prntscr.com/85jodq[^]


I am create new web application and windows application inside Thai OS using VS2010
This is web application asp button click event

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    Dim cn As OleDbConnection
    Dim cmd As OleDbDataAdapter
    Dim dt As New DataTable
    Dim strcmd As String = ""
    cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Program Files (x86)\OCR\UDatabase\MM.mdb; Jet OLEDB:Database Password= Password;")
    strcmd = "SELECT Format(ProcessDate,"yyyy/MM/dd") as PDate FROM tblFile "
    cmd = New OleDbDataAdapter(New OleDbCommand(strcmd))
    cmd.SelectCommand.Connection = cn
    cn.Open()
    cmd.Fill(dt)
    Label1.Text = dt.Rows(0)("ProcessDate")
    cn.Close()
End Sub





My web config is

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
</configuration>



In windows application button click event is

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
   Dim cn As OleDbConnection
        Dim cmd As OleDbDataAdapter
        Dim dt As New DataTable
        Dim strcmd As String = ""
        cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Program Files (x86)\OCR\UDatabase\MM.mdb; Jet OLEDB:Database Password= Password;")
        strcmd = "SELECT Format(ProcessDate,"yyyy/MM/dd") as PDate FROM tblFile "
        cmd = New OleDbDataAdapter(New OleDbCommand(strcmd))
        cmd.SelectCommand.Connection = cn
        cn.Open()
        cmd.Fill(dt)
        Label1.Text = dt.Rows(0)("ProcessDate")
        cn.Close()

End Sub



in Access db got this value in tblFile table

ID	 ProcessDate	 
200	 14/8/2015 4:41:51 PM	



In windows application same US format date showing,but in web application it show Thai date format.

Note: i am running in Thai OS with VS2010 ,need to specify and date culture in web config ?

Pls check this link for exe and web application result
https://www.dropbox.com/s/3b3ano2xnqph0ls/thaios.wmv?dl=0[^]

解决方案

You better install UTC time in the database, use DateTime.ToUniversalTime Method[^] to convert local datetime to UTC. when client application need to show datetime, you can convert UTC time back to local datetime using DateTime.ToLocalTime Method[^]


If I understand the situation correctly the underlying type in the database is correctly Date/Time but in inserts and updates you provide the value as a string literal.

If this is the case, you should use parameters since they help you to overcome such conversions problems. With parameters you can set the parameter value with the regional settings that apply at client side and they are converted properly when added to the database.

Depending what technology you use the parameter classes may be different, but if you operate with OLE DB, have a look at OleDbParameter[^]


The best way to store date data type in MS Access database is to use ISO format[^].

For further information, please see: How to store, calculate, and compare Date/Time data in Microsoft Access [^]


这篇关于如何在访问db中将其他语言日期转换为vb中的美国格式日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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