用于Date数据类型的SqlDataReader进入maskedtextbox [英] SqlDataReader for Date data-type into maskedtextbox
问题描述
您好我有这个SqlDataReader,它将日期数据类型从SQL DB读入带掩码MM / DD / YYYY的maskedTextBox但是当日期是例如05/05/2005它会在Windows XP上的这个55/20 / 05__这样的掩码文本框中显示。
是否有防止这种情况的解决方案?
在Windows 8上它被读作5_ / 5_ / 2005这是正确的
Hello I have got this SqlDataReader which reads date datatype from SQL DB into maskedTextBox with mask MM/DD/YYYY but when the date is e.g. 05/05/2005 it would be displayed in maskedtextbox like this 55/20/05__ on Windows XP .
Is there solution to prevent this?
On Windows 8 it is read as 5_/5_/2005 which is correct
string query = "SELECT * FROM events WHERE name='" + selectedrow + "' AND year='" + selectedyear+ "'";
SqlCommand command= new SqlCommand(query, con);
con.Open();
SqlDataReader read= command.ExecuteReader();
if (read.Read())
{
object nulldate = (maskedTextBox2.Text = read.IsDBNull(24) ?
string.Empty : read.GetDateTime(24).ToShortDateString()); } con.Close
推荐答案
在MSDN中,声明:
In MSDN, it is stated that:
ToShortDateString方法返回的字符串是区分文化的。它反映了当前文化的DateTimeFormatInfo对象定义的模式。例如,对于en-US文化,标准的短日期模式是M / d / yyyy;对于de-DE文化,它是dd.MM.yyyy;对于ja-JP文化,它是yyyy / M / d。特定计算机上的特定格式字符串也可以自定义,以便它与标准的短日期格式字符串不同。
The string returned by the ToShortDateString method is culture-sensitive. It reflects the pattern defined by the current culture's DateTimeFormatInfo object. For example, for the en-US culture, the standard short date pattern is "M/d/yyyy"; for the de-DE culture, it is "dd.MM.yyyy"; for the ja-JP culture, it is "yyyy/M/d". The specific format string on a particular computer can also be customized so that it differs from the standard short date format string.
我建议强制执行预定义的将数据插入数据库并检索数据时的DateTime格式,例如:
I would recommend enforcing a predefined DateTime format in inserting data to DB and in retrieving it such as:
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] dateValues = { "30-12-2011", "12-30-2011",
"30-12-11", "12-30-11" };
string pattern = "MM-dd-yy";
DateTime parsedDate;
foreach (var dateValue in dateValues) {
if (DateTime.TryParseExact(dateValue, pattern, null,
DateTimeStyles.None, out parsedDate))
Console.WriteLine("Converted '{0}' to {1:d}.",
dateValue, parsedDate);
else
Console.WriteLine("Unable to convert '{0}' to a date and time.",
dateValue);
}
}
}
// The example displays the following output:
// Unable to convert '30-12-2011' to a date and time.
// Unable to convert '12-30-2011' to a date and time.
// Unable to convert '30-12-11' to a date and time.
// Converted '12-30-11' to 12/30/2011.
您可以在MSDN中找到更多信息:http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ]
这篇关于用于Date数据类型的SqlDataReader进入maskedtextbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!