简体   繁体   English

C#转SQL-日期格式

[英]C# to sql - date format

Table - tblData (Data for Jan.): 表格-tblData(一月份的数据):

This is how I see the data with select * from tblData 这就是我使用select * from tblData查看数据的select * from tblData

ID  Tag     Date
1   OB    2014-07-01
2   OC    2014-07-01
3   AB    2014-07-01

Trying to achieve the same from C#, but this is not working. 试图从C#中实现相同目的,但这不起作用。

DateTime dtFrom = Convert.ToDateTime(txtFromDate.Text.Trim(),System.Globalization.
                  CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);

DateTime dtTo = Convert.ToDateTime(txtToDate.Text.Trim(),System.Globalization.
                CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);

gridview1.DataSource = objDAL.GetData(dtFrom,dtTo);
gridview1.DataBind(); 

Here's the SQL Server 2008 R2 stored procedure 这是SQL Server 2008 R2存储过程

    Alter Procedure GetData
    (
      @DateFrom Date, @DateTo Date
    )  
    As
    Begin
        Select * from tblData
        where date between @DateFrom and @DateTo
    End

No rows are returned with the above query, and I believe this could be due to the date formats. 上述查询没有返回任何行,我相信这可能是由于日期格式所致。 How can this be solved? 如何解决呢? Not sure if it has to be changed in code or in the stored procedure. 不知道是否必须在代码或存储过程中对其进行更改。

EDIT: 编辑:

  • The dates in the textboxes are - 08-01-2013 文本框中的日期为- 08-01-2013
  • In code behind dtFrom becomes - 1/8/2013 12:00:00 AM dtFrom后面的代码中- 1/8/2013 12:00:00 AM年1月1/8/2013 12:00:00 AM
  • The datatype for 'date' column is just date and not datetime “日期”列的数据类型只是日期,而不是日期时间

GetData in DAL DAL中的GetData

public DataTable GetData(DateTime dtfrom, DateTime dtto)
{
  var qry = from p in MyDB.GetData(dtfrom,dtto)
            select new
             {
               id = p.id,
               tag = p.tag,
               date = p.date
             }
  dt = qry.ToDataTable();
  return dt;
}

The problem's not with the dal code. 问题与dal代码无关。 It executes fine. 它执行得很好。 The row count is 0. So I'm sure its the diff. 行计数为0。因此,我确定其为diff。 date formats. 日期格式。

1) Try this in the SP. 1)在SP中尝试。

Select * from tblData
Where
( [date] >= @DateFrom and [date] <= @DateTo )

See if it helps. 看看是否有帮助。

2) Change your SP to accept DateTime parameters, otherwise somebody (DAL or below) is probably implicitly converting the DateTime values from the UI to Date values. 2)更改您的SP以接受DateTime参数,否则有人(DAL或以下)可能会将UI的DateTime值隐式转换为Date值。

Use can use CONVERT function for converting the date as below.. 使用可以使用CONVERT函数转换日期,如下所示。

Alter Procedure GetData
(
  @DateFrom Date, @DateTo Date
)  
As
Begin
  set @DateFrom = CONVERT(DATE, @DateFrom, 101)
  set @DateTo = CONVERT(DATE, @DateTo , 101)

  Select * from tblData
  where [date] between @DateFrom and @DateTo
End

for more date conversation format use this link . 欲了解更多日期对话格式,请使用此链接

When calling your procedure inside MyDB.GetData(dtFrom,dtTo) pass dtFrom and dtTo as strings. MyDB.GetData(dtFrom,dtTo)调用过程时MyDB.GetData(dtFrom,dtTo)dtFromdtTo作为字符串传递。 For example - 例如 -

//I am assuming you are using SqlCommand To connect to DB
cmd.Parameters.AddWithValue("@DateFrom", dtFrom.ToString("yyyy-MM-dd"));
cmd.Parameters.AddWithValue("@DateTo", dtFrom.ToString("yyyy-MM-dd"));

OR, 要么,

cmd.Parameters.AddWithValue("@DateFrom", dtFrom.ToString("dd-MMM-yyyy"));
cmd.Parameters.AddWithValue("@DateTo", dtFrom.ToString("dd-MMM-yyyy"));

sql server can read this string values and convert them to date correctly. sql server可以读取此字符串值并将其正确转换为date

Check the date format for your query in both C# and in SQL. 在C#和SQL中检查查询的日期格式。 Sometimes the problem occurs when there's a mismatch of date format, a common mistake is that either of the query includes time and the other does not. 有时,当日期格式不匹配时会发生问题,一个常见的错误是查询中的任何一个都包含时间而另一个不包含时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM