儲存數據時出現:從 字元字串转换成日期及/或时间时,转换失败,我是業余愛好新手,求解決方法
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Form1.a1 = True Then
If ComboBox2.Text = "" Then MsgBox("有欄位未填數據") Else Dim constr As String
constr = ("Data Source=mark-pc\SQLEXPRESS;Initial Catalog=engineer;Integrated Security=False;User ID=sa;Password=123456;") sqlcon.ConnectionString = constr sqlcon.Open()
sqlcmd.CommandText = "select * from mjbm" sqlcmd.Connection = sqlcon
Dim myadapter As New SqlDataAdapter
myadapter.SelectCommand = sqlcmd myadapter.Fill(myds) Dim i As Integer = 0 Dim j As Boolean = False For i = 0 To myds.Tables(0).Rows.Count - 1 Step 1 If TextBox1.Text = myds.Tables(0).Rows(i)(0) Then j = True End If Next If j = True Then MsgBox("已有此項了哦!") sqlcon.Close() Else sqlcmd.CommandText = "insert into mjbm(類別,編碼,結構,名稱,開模廠商,日期,適用客戶,適用品名,編碼者) values('" & (ComboBox1.Text) & "','" & TextBox1.Text & "','" & ComboBox2.Text & "','" & TextBox2.Text & "','" & ComboBox3.Text & "','" & DateTimePicker1.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & ComboBox4.Text & "')"
sqlcmd.Connection = sqlcon sqlcmd.ExecuteNonQuery() If MessageBox.Show("添加成功是否繼續添加", "成功", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) = Windows.Forms.DialogResult.Yes Then
Me.Refresh()
ComboBox1.Text = "" ComboBox2.Text = "" ComboBox3.Text = "" ComboBox4.Text = "" TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = ""
TextBox4.Text = ""
sqlcon.Close() Else
sqlcon.Close() Me.Close() End If End If End If 日期字段你设了datetime类型,插了string类型了吧? DateTimePicker1.Text=》 转换下 DateTimePicker1.Value.ToString("yyyy-MM-dd hh:MM:ss") 或者 datetimepicker1.customformat="yyyy-MM-dd hh:MM:ss" ; 应该是 DateTimePicker1.Text 的日期格式和数据库日期格式不一致,改用 DateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") 试试
|