Re: Insert date into a datetime column in Visual Basic 2008

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I noted your canned 'wrong discussion group'. I am using ADO.net so I guess
I should go to the ADO.net discussion group although I think the issues are
more or less much the same. There were no answers to this issue there.

The cast... was my attempt to try anything after using the 'dte' directly
but nonetheless your response indicated that I needed to understand the use
of literal strings in sql commands. As an axiomatic student, I would be
grateful if you send me a link to edify. I present a more complete code to
permit a better understanding of the issue:

Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim cmdString As String
Dim dte As System.DateTime


dte = DateTime.Now.ToString
cmdString = "Insert insertTB([date], [close]) Values ('dte',
'Fnumber')"
conn = New SqlConnection("Data Source=WINALL;Initial
Catalog=DataFill;Integrated Security=True")
cmd = New SqlCommand(cmdString, conn)
conn.Open()

cmd.ExecuteNonQuery()
conn.Close()

I found a 'solution' at (albeit an access database which could be easily
adjusted to SQL via the importation of the relevant library ie.
System.Data.SQLClient and changes to some of the statements eg OleDbCommand)
:

http://www.vbdotnetheaven.com/UploadFile/Ashish1/MSaccessDateInsertInVB11082005000729AM/MSaccessDateInsertInVB.aspx>

Essentially, the same issues are involved, that is how to insert a date into
a datetime db. However, I was not successful in copying the example, possibly
due to the terse explanations given.

"Bob Barrows" wrote:

Gum wrote:
I need to insert a date (i.e., "2009/05/12 02:46" or
system.datetime.tostring) into a datetime column using ADO. I got an
error saying that there is a problem converting the string into
datetime.
I am using a SQL string "INSERT insertTB([date],[close])
values('cast(dte as datetime)', 'Fnumber')"
How can I do this insert using the most efficient method?

Why would you think the string 'cast(dte as datetime)' could be inserted
into a datetime column? You do realize that's a literal string, don't you? I
think more of your code is necessary to enable someone to solve your
problem.

Are you using ADO or ADO.Net? If the latter:
***canned wrong-newsgroup reply************************
There was no way for you to know it (except maybe by browsing through some
of the previous questions in this newsgroup before posting yours - always a
recommended practice) , but this is a classic (COM-based) ADO newsgroup.
ADO.Net bears very little resemblance to classic ADO so, while you may be
lucky enough to find a dotnet-knowledgeable person here who can answer your
question, you can eliminate the luck factor by posting your question to a
group where those dotnet-knowledgeable people hang out. I suggest
microsoft.public.dotnet.framework.adonet.
..******************************************************************


--
Microsoft MVP - ASP/ASP.NET - 2004-2007
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"



.



Relevant Pages

  • RE: Query to DB2
    ... Since the ADO code took considerable sniffing around and piecing together, ... Extract data from a Database to Excel ... Dim cn As New ADODB.Connection ... Dim sProv As String, sDS As String, sCon As String, sTable As String, ...
    (microsoft.public.excel.programming)
  • RE: ODBC query in VB code Need HELP
    ... Everything for ADO is in the first 2 messages that I gave you. ... Since your pass-through query already exists (including the ... Dim STRSQL As String ...
    (microsoft.public.access.formscoding)
  • Re: Problem adding record using ADO
    ... >>> Dim strDBPath As String ... >>> Dim strInsertSQL As String ... Also useful to add error handling and collect *ALL* of the ADO errors.... ...
    (microsoft.public.vb.database.ado)
  • Re: Luddites ! ; forward into the past ! (ADO/DAO)
    ... I am the harbinger of doom for those who continue to try to use traditional ADO with .NET. ... ' Dim Conn As New ADODB.Connection ... Dim sql As String ... Private Sub Button1_ClickHandles Button1.Click ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Insert date into a datetime column in Visual Basic 2008
    ... system.datetime.tostring) into a datetime column using ADO. ... error saying that there is a problem converting the string into ... Why would you think the string 'cast' could be inserted ... Are you using ADO or ADO.Net? ...
    (microsoft.public.data.ado)