Wednesday, March 28, 2012

How to get the system date and time into database

Hi,

I m using ASP.NET with C#.
I m having one field which should store the datetime of the system.

The datetime should be automatically stored for that entry when the user submits that record on the click event.
Then it should display the date time on the gridview from database.

I m using sqlserver 2005 and i have created the stored procedure for the insert command.

Thisis the sample sp what should be written here to insert system date timeautomatically when the user submits the asp.net form ?

Is there any code for writing directly in stored procedure or asp.net coding page...


ALTER PROCEDURE [dbo].[StoredProcedure1]

@.salesid INT OUTPUT,
@.salesdate datetime,
@.customername varchar(20)

AS
BEGIN
SET NOCOUNT ON

BEGIN
INSERT INTO sales (customername)
VALUES (@.customername)
SELECT @.companyid = SCOPE_IDENTITY()
END

SET NOCOUNT OFF
END

 
Thanxs in advance...

Hi,

use getdate() function of sql server.

for more info look here

http://msdn2.microsoft.com/en-us/library/ms188383.aspx

|||

You have a choice as to where to use the getdate() function (which returns the current system date and time).

You can actually define it on a column in the table itself so that you don't have to set it programmatically. Here is sample syntax:

create table sales (sales_id int identity, customer_name varchar(30) not null,sales_date datetime default getdate() not null)

Alternatively, within the stored procedure assign your variable with the function call. Here is sample syntax:

select @.SystemDate = getdate()

No comments:

Post a Comment