Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb_dotnet thread: writing SQL Server 7 dates


Message #1 by "Bob Clegg" <bclegg@a...> on Mon, 2 Dec 2002 01:50:35
Hi Gabriel,
Due to some horrible sin that I committed in a previous life, when I use 
OADate it writes a date precisely 2 days in the future.
My final solution(s) (client and server side)
 are using the parameters collection of the command object. (As 
per 'Updating Data in a SQL Database' in '.Net Framework Developers Guide')
Code follows. 
Thanks again to all who helped on this.
Regards
Bob
Public Sub Writebatch(ByVal batchNumber As Long)
        Dim j As Long
        Dim conn As New SqlClient.SqlConnection(mstrConnectionString)
        Dim cmdConn As New SqlClient.SqlCommand()
        Dim strSQL As String

        cmdConn.Parameters.Add(New SqlParameter("@id", SqlDbType.Int))
        cmdConn.Parameters.Add(New SqlParameter("@createdate", 
SqlDbType.DateTime))

        Try
            cmdConn.Parameters("@id").Value = batchNumber
            cmdConn.Parameters("@createdate").Value = Now()
            '*****CLIENT SIDE STRING************
            'strSQL = "insert into batch(id,create_date) values
(@id,@createdate)"
            '********SERVER SIDE 
STRING*************************************
            strSQL = " Exec WriteBatch @id,@createdate"

            cmdConn.CommandText = strSQL
            cmdConn.Connection = conn
            cmdConn.Connection.Open()
            j = cmdConn.ExecuteNonQuery()
        Catch obja As System.Exception
            MsgBox("dt.writebatch error " & obja.Message)
        Finally
            cmdConn.Connection.Close()
        End Try
    End Sub

> Hi there,

My personal opinion is that we should not rely on
regional settings or sqlserver settings.
Our application must be indipendent of all that .

Having had the problem myself I have done a bit of
research and so far the best way to handle this
"Yankee Stuff" just kidding!!!!is to use the 
toOADate.

Thanks
Gabriel 



 --- Bob Clegg <bclegg@a...> wrote: > Hi
Richard,
> Can do in this case, in fact it is preferable. But I
> am concerned that I 
> can generate a 'valid' date in an SQl string and
> have the datebase write 
> an invalid date. This problem has been around in one
> form or another since 
> the dark ages. I had hoped that .net talking to SQL
> Server would be immune.
> ie 
> msgBox (Now.shortdatestring) shows 4/12/2002
> The record written by the  j =
> cmdConn.ExecuteNonQuery() shows 12/4/2002
> A manual edit of the table inserting 4/12/2002
> works.
> A copy of the strSQL string used in  SQL analyser
> works.
> To me, this is a bug in .net
> regards
> Bob 
> > Why not build a stored procedure and just pass a
> valid date type into the
> appropriate command parameter?
> 
> -----Original Message-----
> From: Bob Clegg [mailto:bclegg@a...]
> Sent: Monday, December 02, 2002 1:51 AM
> To: pro_VB_dotnet
> Subject: [pro_vb_dotnet] writing SQL Server 7 dates
> 
> 
> Hi,
> Our date format is dd/mm/yyyy.
> If I construct an SQL string to write a date it
> appears to be written in
> the database as mm/dd/yyyy
> eg
> strSQL = "insert into batch(id,create_date) values("
> & batchNumber & ",'"
> & Now().ToShortDateString & "')"
>             cmdConn.Connection = conn
>             cmdConn.CommandText = strSQL
>             cmdConn.Connection.Open()
>             j = cmdConn.ExecuteNonQuery()
> Writes "12/2/2002 to the database.
> The database is 'correctly' set up as far as I can
> tell.
> eg If I edit the above record to say '15/2/2002 it
> is allowed whereas
> 12/15/2002 fails
> My regional settings appear to be correct as far as
> I can tell.
> Currently I am building my dates by hand. There has
> got to be a better way.
> Thanks
> Bob
> ---
> Change your mail options at
> http://p2p.wrox.com/manager.asp or
> to unsubscribe send a blank email to
> 
> 
> ---
> Change your mail options at
> http://p2p.wrox.com/manager.asp or 
> to unsubscribe send a blank email to
 

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

  Return to Index