如何在日期时间控件中设置上一个日期 [英] How Do I Set Previous Date In Datetime Control

查看:107
本文介绍了如何在日期时间控件中设置上一个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在datetime控件中从数据库中检索以前的日期。



我尝试的是

 queryString =   SELECT Startdate FROM master WHERE srno = + t +  ; 
while (dr.Read())
{
DateTime stdate = Convert.ToDateTime(dr.GetValue( 2 )的ToString());
this .dtpBlockStartDate.Value = new DateTime(stdate.Year,stdate.Month,stdate 。天);
}



日期时间控件显示当前日期而不是数据库中的日期。

解决方案

1.命令中的参数 t 应该是你想要的日期,如果你想要昨天应该是:

DateTime t = DateTime.Now.AddDays(-1);



2.然后你应该像这样改进你的阅读代码:

  if (dr.Read())
{
this .dtpBlockStartDate。值=(DateTime)dr [ Startdate];
}


使用日期和时间对象时。然后不要将它转换为字符串。

只有当你想要显示给用户时才转换为字符串。



如果表列Startdate是MS SQL中的日期时间类型。您可以直接使用值:

  var  sqlConnection =  new  SqlConnection(sqlConnectionString); 
sqlConnection.Open()

var sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText = SELECT Startdate FROM master WHERE srno = @ srno;
sqlCommand.Parameters.AddWithValue( srno,t);

object result = sqlCommand.ExecuteScalar();

if (result!= null
{
dtpBlockStartDate.Value =(DateTime)结果;
}





请记住使用参数化的sql语句:-)

http://blog.codinghorror.com/give-me-parameterized-sql-or-give- me-death / [ ^ ]


你好,

如果你的DataType是DateTime&使用C#。尝试使用以下代码来设置值。

需要指定DatePicker控件的CustomFormat属性。

 dtpBlockStartDate.CustomFormat =   dd-MMM-yyyy; 
dtpBlockStartDate.Value = Convert.ToDateTime(ds.Tables [ 0 ]。行[ 0 ] [ 0 ]);









ds指数据集&我认为它在第一栏。



尝试调试它。有些你可能会把它重置为Currrent Date



如果有任何问题然后粘贴你的完整代码

希望它能帮助你

快乐编码: - )


I want to retrieve previous date from database in datetime control.

what I tried is

queryString = "SELECT Startdate FROM master WHERE srno="+ t +"";
while (dr.Read())
{                    
    DateTime stdate = Convert.ToDateTime(dr.GetValue(2).ToString());
    this.dtpBlockStartDate.Value = new DateTime(stdate.Year , stdate.Month , stdate.Day);
}


datetime control is displaying current date and not the date from database.

解决方案

1.The parameter t from your command should be the date that you want, if you want yesterday should be:
DateTime t = DateTime.Now.AddDays(-1);

2.Then you should refine your reading code like this:

if(dr.Read())
{
    this.dtpBlockStartDate.Value = (DateTime)dr["Startdate"]; 
}


When you are working with date and time objects. Then do not convert it to a string.
Only convert to a string when you want to show it to the user.

If table column "Startdate" is a datetime type in MS SQL. You can use the value direct like:

var sqlConnection= new SqlConnection(sqlConnectionString);
sqlConnection.Open()

var sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText = "SELECT Startdate FROM master WHERE srno=@srno";
sqlCommand.Parameters.AddWithValue("srno", t);

object result = sqlCommand.ExecuteScalar();

if (result != null)
{
  dtpBlockStartDate.Value = (DateTime)result;
}



Remember to use parameterized sql statement :-)
http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/[^]


Hello ,
If you DataType is DateTime & Using with C#.Try the Following Code to set a value.
There is a CustomFormat Property for the DatePicker control that is needed to be specified.

dtpBlockStartDate.CustomFormat = "dd-MMM-yyyy";
dtpBlockStartDate.Value =Convert.ToDateTime(ds.Tables[0].Rows[0][0]);





ds refers to dataset & i am suposing that it is in first column.

Try To debug it .SomeWhere you might be resetting it to Currrent Date.

If any Problem Then Paste Your Complete Code
Hope It Helps You
Happy coding :-)


这篇关于如何在日期时间控件中设置上一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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