如何解决日期时间问题 [英] how to solve datetime problem

查看:70
本文介绍了如何解决日期时间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有1列为datetime但是当我在运行时进入时,

显示错误,无法从char数据类型转换为datetime数据类型。



即使我做了convert.todatetime(textbox8.text)



但仍显示错误



PLZ任何人都可以帮助我,



i将有义务。

谢谢

sudeshna



##编辑##代码从评论中添加,因此可读。



i have 1 column as datetime but when i am entering at runtime,
its showing error that cant convert from char datatype to datetime datatype.

even i did convert.todatetime(textbox8.text)

but still showing error

plz can anyone help me,

i will be obliged.
thanks
sudeshna

##Edit## Code added from comment so it is readable.

Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
        Dim da As New SqlDataAdapter
        Dim cmd As New SqlCommand
        Dim ds As New DataSet
        'Dim dr As SqlDataReader
        cmd.Connection = cn
        cn.Open()
        cmd.CommandText = "Update facsort set " & "name = '" & TextBox1.Text & "', " & "type='" & TextBox2.Text & " '," & "shape='" & TextBox3.Text & " '," & "description='" & TextBox4.Text & " ' ," & "qty='" & TextBox5.Text & " '," & "wt_gvn = '" & TextBox6.Text & "', " & "issue_dt = '" & Convert.ToDateTime(TextBox7.Text) & "'," & "wt_rtn='" & TextBox8.Text & "'," & "return_dt='" & Convert.ToDateTime(DateTimePicker1.Value) & "'" & "Where lot_no = '" & ComboBox1.SelectedItem & "'"
        cmd.ExecuteNonQuery()
        MsgBox("Data successfully updated", MsgBoxStyle.Information)
        Me.FacsortTableAdapter.Insert(ComboBox1.Text, TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text, Convert.ToDateTime(TextBox7.Text), TextBox8.Text, Convert.ToDateTime(DateTimePicker1.Value))
        Me.FacsortTableAdapter.Fill(Me.RecordDataSet.facsort)
        Me.TextBox8.Text = ""
        Me.DateTimePicker1.Text = ""

推荐答案

转换t字符串数据到目前为止您必须使用以下方法的时间值

i希望这会让你感到满意.....



格式( CDate(DateTimePicker1.Value),MM / dd / yyyy)
to convert String Data Value in to date time you have to use following methodology
i hope this will helf you.....

Format(CDate(DateTimePicker1.Value), "MM/dd/yyyy")


一般来说,当日期是MM / dd / yyyy格式并且它试图转换为dd / MM时会出现此问题/ yyyy格式。如果日期大于12,则会出现错误。您可以使用字符串操作交换dd和MM,然后在转换时转换或使用美国文化。希望这很有用。
Generally this problem occurs when the date is in MM/dd/yyyy format and its trying to convert to dd/MM/yyyy format. The error ocures if day is greater than 12. You can swap dd & MM using string manupalation and then convert or use US culture while convertion. hope this is useful.


Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
       Dim da As New SqlDataAdapter
       Dim cmd As New SqlCommand
       Dim ds As New DataSet
       'Dim dr As SqlDataReader
       cmd.Connection = cn
       cn.Open()
       'cmd.CommandText = "insert into sort(lot_no,name,type,shape,size,place,weight,issue_dt) values (' " & ComboBox1.Text & " ',' " & TextBox2.Text & " ',' " & TextBox3.Text & " ',' " & TextBox4.Text & " ',' " & TextBox5.Text & " ',' " & TextBox6.Text & " ',' " & TextBox7.Text & " ','" & DateTimePicker1.Value & " ');insert into finalsort(lot_no,name,type,shape,size,place,weight,issue_dt,return_dt) values (' " & ComboBox1.Text & " ',' " & TextBox2.Text & " ',' " & TextBox3.Text & " ',' " & TextBox4.Text & " ',' " & TextBox5.Text & " ',' " & TextBox6.Text & " ',' " & TextBox7.Text & " ',' " & TextBox8.Text & " ',' " & DateTimePicker1.Value & " ')"
       cmd.CommandText = "insert into finalsort(lot_no,name,type,shape,size,place,weight,issue_dt,return_dt) values (' " & ComboBox1.Text & " ',' " & TextBox2.Text & " ',' " & TextBox3.Text & " ',' " & TextBox4.Text & " ',' " & TextBox5.Text & " ',' " & TextBox6.Text & " ',' " & TextBox7.Text & " ',' " & Format(CDate(TextBox8.Text), "MM/dd/yyyy") & " ',' " & Format(CDate(DateTimePicker1.Value), "MM/dd/yyyy") & " ')"
       cmd.ExecuteNonQuery()
       MsgBox("Data successfully added", MsgBoxStyle.Information)

       'Me.FinalsortTableAdapter.Insert(ComboBox1.Text, TextBox2.Text, TextBox3.Text, Convert.ToDouble(TextBox4.Text), TextBox5.Text, TextBox6.Text, TextBox7.Text, TextBox8.Text, Format(CDate(DateTimePicker1.Value), "MM/dd/yyyy"))
       'Me.FinalsortTableAdapter.Fill(Me.RecordDataSet.finalsort)
       Me.ComboBox1.Text = ""
       Me.TextBox2.Text = ""
       Me.TextBox3.Text = ""
       Me.TextBox4.Text = ""
       Me.TextBox5.Text = ""
       Me.TextBox6.Text = ""
       Me.TextBox7.Text = ""
       Me.TextBox8.Text = ""
       Me.DateTimePicker1.Text = ""


这篇关于如何解决日期时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆