sql 2008 DATE类型向我显示DATE&在Visual Studio中的时间 [英] sql 2008 DATE type showes me DATE & TIME in visual studio

查看:83
本文介绍了sql 2008 DATE类型向我显示DATE&在Visual Studio中的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SQLserver 2008,并且我的字段具有DATE类型,但是在我的程序的Visual Studio中,当我读取可能的数据时,它显示了DATE&时间我该怎么办?我尝试在程序的SELECT查询中强制转换字段,但它使我感到有些错误,如何解决此问题?

这是我的背后代码:

I''m using SQLserver 2008 and my fields have DATE type but in visual studio in my program when I read may data it shows me DATE & TIME what should I do? I try to cast my fields in SELECT query of my program but it qives me some error,how can I solve this problem?

this is my behind code:

<pre>  protected void Page_Load(object sender, EventArgs e)
    {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(Class1.CnnStr);
            SqlDataReader reader;
    
    
            cmd.CommandText = "select ContractStartDate,MandateValidDate from table where BrokerName=@BrokerName";
            cmd.Connection.Open();
            cmd.Parameters.AddWithValue("@BrokerName", BrokerName_txt.Text);
            reader = cmd.ExecuteReader();
    
            if (reader.Read())
            {
               
               
                ContractStartDate_txt.Text = reader["ContractStartDate"].ToString();
                MandateValidDate_txt.Text = reader["MandateValidDate"].ToString();
                reader.Close();
            }
            cmd.Connection.Close();
    
        }

,由于我想在读取数据后保存数据,因此出现错误:

从字符串转换日期和/或时间时转换失败.

and because I want to save data after read them it gives me error:

Conversion failed when converting date and/or time from character string.

推荐答案

更改此部分:
Change this part:
ContractStartDate_txt.Text = reader["ContractStartDate"].ToString();
                MandateValidDate_txt.Text = reader["MandateValidDate"].ToString();



收件人:



TO:

ContractStartDate_txt.Text = reader["ContractStartDate"].ToString("MM:dd:yyyy");
                MandateValidDate_txt.Text = reader["MandateValidDate"].ToString("MM:dd:yyyy");



如果仍然无法解决,请在这里供您参考:
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx [ ^ ]

问候,
爱德华



If this doesn''t fix, here for your reference:
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx[^]

Regards,
Eduard


这篇关于sql 2008 DATE类型向我显示DATE&amp;在Visual Studio中的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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