Sunday, March 11, 2012

datetime format

Hello:

Hello:
I have some code in a asp.net function in C# like this:


DateTime datetime = DateTime.Now;

SqlCommand CommandEvent = new SqlCommand("spAddNewEvents", Connection);
CommandEvent.Transaction = Trans;
CommandEvent.CommandType = CommandType.StoredProcedure;

*I try First : CommandEvent.Parameters.Add("@.date", datetime);
*I try Secound: CommandEvent.Parameters.Add("@.date", SqlDbType.DateTime,
8).Value = datetime;


and have some storeprocedure with this code

CREATE PROCEDURE dbo.spAddEvents
(
@.guid uniqueidentifier,
@.language char (2),
@.date as datetime,
@.eventId as varchar (50),
@.userid as varchar (20)
)
AS
execute('insert into tblEvents'+@.language+'( guid, [date], [id], userid)
values('''+@.guid+''','''+@.date+''','+@.eventId+','''+@.userId+''')')

GO


The problem is when I try to insert a new event. The event insert are fine,
but the datetime's secound in tblEvents always is 00, and I check the
datetime variable to insert and have secound different that 00.

the table definition is Data Type: datetime and Length: 8, how must be?

I run Profiler and a I get this:


exec spAddEvents @.Guid = 'C879D062-C268-4A3E-8D58-1937B7612EC2', @.language = N'ES', @.date = 'May 6 2004 11:29:58:140PM', @.eventId = 6, @.userid = N'anibal'

Best regards.
Owen.That's odd. Try writing your stored procedure without using a dynamic string. In this case, you don't need to use a dynamic statement. It might be causing trouble there. Also, if you're just putting in an insert date (not depending on any logic in your code), use getdate() within your stored procedure and forget about passing in the date. No need to generate that in your code.

No comments:

Post a Comment