简体   繁体   中英

How to search SQL Server database using C#

I have a SQL Server table Registration with name and datetime, I want to search the data with datetime which is less then datetime.now .

SqlDataAdapter cs = 
    new SqlDataAdapter("Select * from Registration where Date > DateTime.now")

I want that datetime is less then today.

The T-SQL built-in function getdate() returns the current server date/time.

Therefore your query should be, SELECT * FROM Registration WHERE [Date] < getdate() .

Using this in your code would be:

SqlDataAdapter cs = new SqlDataAdapter("Select * from Registration where [Date] < getdate()")

您可以使用SQL GETDATE()执行此操作:

SqlDataAdapter cs = new SqlDataAdapter("Select * from Registration where Date < GETDATE()")

使用GETDATE()获取查询中的当前日期。

SqlDataAdapter cs = new SqlDataAdapter("Select * from Registration where Date < GETDATE()");

This may work... if you want to use .NET DateTime, that will be the date of the local system.

     SqlDataAdapter cs = new SqlDataAdapter(
           "SELECT * FROM [Registration] 
            WHERE [Date] < '" + DateTime.Now.ToShortDateString() +"';");

     //output: SELECT * FROM [Registration] WHERE [Date] < '2/22/2013';

It is going to depend upon the data type of the [date] column, if it is string or datetime or something else. If it is YYYYmmdd, then you can manipluate the DateTime.Now output to match.

Its very simple..

if you want to use sql current datetime use SELECT * FROM Registration WHERE [Date] < getdate()

and if you want your application current datetime use

"SELECT * FROM Registration WHERE [Date] < '+Datetime.Now.Tostring() +'"

Also try to use parameterized query if you want second option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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