如何从nvarchar(64),VB.NET中调用月和日 [英] How to call month and day from nvarchar(64), VB.NET

查看:71
本文介绍了如何从nvarchar(64),VB.NET中调用月和日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的兄弟姐妹们,

请帮助我实现这个目标我想在我们的门户网站创建一个生日快乐专栏,在Vb.net中完成。

出生日期的值以nvarchar(64)(dd / MM / yyyy)保存我想将我的Datalist绑定到当前日期和月份。下面是我的代码。请帮助我,并提前致谢。



我的尝试:



Dear Brothers and Sisters,
Please help me to achieve this goal i want to create a Happy Birthday Column in our web portal with is done in Vb.net.
The Value of Date of birth is save in nvarchar(64) (dd/MM/yyyy) i want to bind my Datalist to the current day and month. below is my code. Please help me and thanks in advance.

What I have tried:

Dim sConnStrEMPBIRTH As String = ConfigurationManager.ConnectionStrings("SecurePerfectHistoryConnectionString").ConnectionString

Dim conEMPBIRTH As New SqlConnection(sConnStrEMPBIRTH)
conEMPBIRTH.ConnectionString = "Data Source=DBserver;Initial Catalog=SecurePerfect;Persist Security Info=True;User ID=XXX;Password=XXX"


        Dim cmdEMPBIRTH As New SqlCommand("SELECT value1, value8 FROM [UserFieldTable] WHERE ([value8] = @value8)", conEMPBIRTH)
        cmdEMPBIRTH.Parameters.Add("@value8", SqlDbType.NVarChar).Value = Date.Today.Day And Date.Today.Month

        Dim drEMPBIRTH As SqlDataReader
        conEMPBIRTH.Open()
        drEMPBIRTH = cmdEMPBIRTH.ExecuteReader()
        While drEMPBIRTH.Read()
            fclblemp.Text = drEMPBIRTH(0).ToString()
            fclbldate.Text = drEMPBIRTH(1).ToString()
            
        End While
        conEMPBIRTH.Close()

推荐答案

基本上,不要。

更改数据库,以便在DATE,DATETIME或DATETIME2字段中存储基于日期的信息,并且只使用NVARCHAR用于基于字符串的数据。



问题是基于字符串的数据没有定义的格式,而且非常常见(并且非常难以使用)该字段中的数据不一致。例如,如果字符串是01/02/1982,可能是欧洲:1982年2月1日,或美国:1952年1月2日 - 你无法分辨,因为一旦数据到达数据库,你就丢失了所有上下文谁输入它,以及用户使用的Locale。如果你的系统没有检查用户条目,那么它可能是35 Mars 96或Go and multiply而你不知道他的意思。



即使你现在的数据都是正确的,这并不意味着将来会是这样:使用正确的数据类型可以确保它始终是,因为用户条目的转换是在表示层完成的,并且可以使用正确转换它的区域设置信息。



如果你必须使用字符串 - 每次你尝试使用它们都会让你头疼 - 那么看看 SUBSTRING [ ^ ]和CONVERT [ ^ ]函数。但它会变得丑陋且容易出错。
Basically, don't.
Change your database so that you store date-based info in DATE, DATETIME, or DATETIME2 fields, and only ever use NVARCHAR for string based data.

The problem is that string based datas don't have a defined format, and it's very common (and extremely hard to work with) that the data in the field is not consistent. For example, if the string is "01/02/1982" that could be European: 1st Feb, 1982, or US: 2nd Jan, 1952 - and you can't tell because once the data hits the DB you have lost all context on who entered it, and what Locale the user was using. And if your system doesn't check user entries, then it could be "35 Mars 96" or "Go forth and multiply" and you have no idea what he meant.

Even if you data is all correct now, that doesn't mean it will be in the future: using the correct datatypes ensures that it always will be, because the conversion from user entry is done at the presentation layer and can use the Locale info to convert it correctly.

If you must use strings - and they will give you headaches every time you try to use them - then look at the SUBSTRING[^] and CONVERT[^] functions. But it's going to be ugly and error prone.


这篇关于如何从nvarchar(64),VB.NET中调用月和日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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