我已将所有日期值作为数据库中的日期,但是当我给予valye 09/11 / 2014 ..接受但是当我在-30 / 10/2014给出值时...给出这种错误..为什么..需要帮助...
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'TODO:add data into data base
If ComboBox1.Text <> "" Then
Dim ab As Long
ab = random.Next("10000", "99999")
Dim date1 As Date = Date.ParseExact(TextBox3.Text, "dd/MM/yyyy", Nothing).Date
Dim date2 As Date = Date.ParseExact(TextBox4.Text, "dd/MM/yyyy", Nothing).Date
Dim date3 As Date = Date.ParseExact(TextBox5.Text, "dd/MM/yyyy", Nothing).Date
'Dim date3 As Date = Date.Parse(TextBox5.Text)
Dim date4 As Date = Date.ParseExact(TextBox6.Text, "dd/MM/yyyy", Nothing).Date
If Button1.Enabled = False Then
If TextBox11.Text <> "" And ComboBox1.Text <> "" And TextBox2.Text <> "" And TextBox3.Text <> "" And TextBox4.Text <> "" Then
TextBox10.Text = ab
cm.Close()
cm.Open()
ComboBox1.Text = Replace(ComboBox1.Text, "'", "''")
TextBox1.Text = Replace(TextBox1.Text, "'", "''")
TextBox7.Text = Replace(TextBox7.Text, "'", "''")
TextBox6.Text = Now.Date.ToString("dd/MM/yyyy")
TextBox8.Text = DateTime.Now.ToString("HH:mm:ss")
cmd.CommandText = "insert into worker(faccno,fname,faddr,famcdue,fjoindate,fdate,fattended,fproblem,fsolution,fstatus,fremark,fassign,findate,fintime,fserviceno,falert)values ('" & TextBox11.Text & "', '" & ComboBox1.Text & "','" & TextBox2.Text & "','" & date1.ToString("dd/MM/yyyy") & "','" & date2.ToString("dd/MM/yyyy") & "','" & date3.ToString("dd/MM/yyyy") & "','" & ComboBox4.Text & "','" & TextBox1.Text & "','" & TextBox7.Text & "','" & ComboBox2.Text & "','" & ComboBox6.Text & "','" & ComboBox5.Text & "','" & date4 & "','" & TextBox8.Text & "','" & TextBox10.Text & "','" & ComboBox3.SelectedIndex & "')"
cmd.ExecuteNonQuery()
MessageBox.Show("Information Insertion sucessfull", "Save")
MessageBox.Show(ab, "your service no is")
Button7.PerformClick()
cm.Close()
Else
MessageBox.Show("enter all values then try to save information", "error")
Exit Sub
End If
ElseIf ComboBox1.Text <> "" Then
cm.Close()
cm.Open()
TextBox1.Text = Replace(TextBox1.Text, "'", "''")
TextBox7.Text = Replace(TextBox7.Text, "'", "''")
TextBox6.Text = Now.Date.ToString("dd/MM/yyyy")
TextBox8.Text = DateTime.Now.ToString("HH:mm:ss")
ComboBox4.Text = Replace(ComboBox4.Text, "'", "''") 'TODO:this code replaces single quotes to store in data base
cmd.CommandText = "update worker set faccno= '" & TextBox11.Text & "', fname='" & ComboBox1.Text & "',faddr='" & TextBox2.Text & "',famcdue='" & date1 & "',fjoindate='" & date2 & "',fdate='" & date3 & "',fattended='" & ComboBox4.Text & "',fproblem='" & TextBox1.Text & "',fsolution= '" & TextBox7.Text & "',fstatus='" & ComboBox2.Text & "',fremark='" & ComboBox6.Text & "',fassign= '" & ComboBox5.Text & "',findate='" & date4 & "',fintime='" & TextBox8.Text & "',fserviceno='" & TextBox10.Text & "',falert='" & ComboBox3.SelectedIndex & "' where fserviceno='" & TextBox10.Text & "'"
cmd.ExecuteNonQuery()
MessageBox.Show(" Information Updation sucessfull", "Save")
Button7.PerformClick()
cm.Close()
End If
Else
MessageBox.Show("enter all values then try to save information", "error")
Exit Sub
End If
End Sub
答案 0 :(得分:1)
在解决实际问题之前,我注意到你正在使用字符串连接来形成SQL语句(不要这样做 - 改为使用类型参数!)
关于你发布的问题:有多种可能的原因,但最可能的原因是这一行:
TextBox6.Text = Now.Date.ToString("dd/MM/yyyy")
...您将日期转换为dd/MM/yyyy
格式,但您的SQL服务器可能希望斜杠分隔的日期采用格式MM/dd/yyyy
(默认的1033格式 - 责怪美国人),在这种情况下,30
的月份成分没有意义,因此失败。
如果您使用了参数,那么您就不会遇到这个问题,因为实际的日期值(而不是它的字符串表示形式)被传递到数据库客户端库,然后可以以一种方式为您正确地序列化它永远都有效。