Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have this code:

 


       Dim connString As String = "Provider=SQLOLEDB.1;User Id=sa;Password=password;Initial Catalog=Northwind;server=localhost;Use Encryption for Data=False"
       Dim myconnection As OleDbConnection = New OleDbConnection(connString)

       ' If the connection string is null, use a default.
       Dim date1 As String = Date.Today.ToString("yy-MM-dd")
       Try
           Dim myInsertQuery As String = "INSERT INTO DateTimePayload (Date) Values(date1)"
           Dim myCommand As New OleDbCommand(myInsertQuery)
           myCommand.Connection = myconnection
           myconnection.Open()
           myCommand.ExecuteNonQuery()
           myCommand.Connection.Close()
       Catch
           MsgBox(Err.Description)
       End Try

 

That is all I have so far... I basically just want to store the date in one field, and time in another (time down to the minutes is fine)...

 

How can I do this? with my above code, I get an error about date1 not being able to be there or something..

 

ideas?

 

thanks

Lee

  • Administrators
Posted

You cannot just use .Net variable inside an SQL command like that, you would be much better off looking at using either a parameterised query or a stored procedure that accepts parameters; search these forums for examples of both ways - they crop up a lot in questions here.

Also if you are connection to a SQL server you might want to use the classes under the SqlClient namespace rather than OLEDB as then you will not be going through the OleDb layer but will be using .Net's native sql support.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
 Dim myInsertQuery As String = "INSERT INTO DateTimePayload (Date) Values(date1)"

Your string will be like

INSERT INTO DateTimePayload (Date) Values(date1)

try instead

 Dim myInsertQuery As String = "INSERT INTO DateTimePayload (Date) Values('" & date1 & "')"

and you will get

INSERT INTO DateTimePayload (Date) Values('2005-10-03')

 

hth

/Kejpa

Posted
Trust me you will encounter problems sooner or later due to the formatting of the date using this method - a stored procedure or a parameterised query will be much less error prone.

 

I have the following and i want to format the date and time

 

Dim Parm3 As SqlParameter = .SelectCommand.Parameters.Add("@movedate", SqlDbType.DateTime)

Parm3.Value = movedate.GetDateTimeFormats(("dd-mm-yyyy hh:mm:ss"))

 

that is something wrong with my second line of code, can u pls help me

programing programer program
Posted
Parm3.Value = movedate

If you are using parameters you don't need to worry about the formatting.

 

else it will come out with a overflow problem,

 

I do it by this

Parm3.Value = movedate.Year.ToString() & "-" & movedate.Month.ToString() & "-" & movedate.Day.ToString()

is that any easier way to do it

programing programer program
  • Administrators
Posted

What do you mean by

else it will come out with a overflow problem

 

Are you getting an overflow when you use the code I posted? How is the underlying table column defined (datetime, smalldatetime etc)?

 

You shouldn't need to be formatting the date as a string when using parameters - that is one of the main reasons for using parameters.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
What do you mean by

 

Are you getting an overflow when you use the code I posted? How is the underlying table column defined (datetime, smalldatetime etc)?

 

You shouldn't need to be formatting the date as a string when using parameters - that is one of the main reasons for using parameters.

 

yes, that is an error message pop-up when i use your without format it. The column is defined as DateTime. The problem is solve after i use the toString, i believe that is some way that is better than toString that i can use, hope u can help me.

programing programer program

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...