这是我的代码:
Dim d As DateTime = DateTime.Now.ToString("MM/dd/yyyy")
d = txtDate.Text
Try
Dim sql = "SELECT Date FROM tblTeacherInfo WHERE Date = '" & txtDate.Text & "'"
con = connectDB()
con.Open()
mycommand = New SqlCommand(sql, con)
Dim dr As SqlDataReader = mycommand.ExecuteReader
If dr.Read = True Then
Try
If txtReceived.TextLength < 8 Then
MsgBox("RFID # should be at least 8 characters in length.", MsgBoxStyle.Critical, "String Size Specification")
con.Close()
Else
con.Close()
con.Open()
mycommand = New SqlCommand("insert into tblTeacherInfo (Date) values ('" & txtDate.Text & "')", con)
mycommand.ExecuteNonQuery()
MsgBox("New Patron added to the database.", MsgBoxStyle.Information, "CIETI - Information System")
ds.Tables("tblTeacherInfo").Rows.Clear()
con.Close()
End If
Catch ex As Exception
MsgBox("Data not save.", MsgBoxStyle.Information, "CIETI - Information System")
con.Close()
Exit Sub
End Try
Else
MsgBox("Name Already exist", MsgBoxStyle.Critical, "CIETI - Information System")
ds.Tables("tblTeacherInfo").Rows.Clear()
'Exit Sub
End If
Catch ex As Exception
MsgBox("Please enter numbers only", MsgBoxStyle.Information, "CIETI - Information System")
Exit Sub
End Try
If con.State <> ConnectionState.Closed Then
con.Close()
End If
我在保存Date
时出错,
当我运行它时,它总是转到
将ex捕获为异常“请仅输入数字”。
是否有任何简单的方法可以将日期保存到数据库?
答案 0 :(得分:0)
尝试插入参数。
mycommand = New SqlCommand("insert into tblTeacherInfo [(Date)] values (@Date)", con)
mycommand.Parameters.Add(New SqlParameter With {.ParameterName = "@Date", .SqlDbType = SqlDbType.DateTime, .Value = txtDate.Text})
或者尝试在字段中添加Square Brackets
[]
,因为Date
是SQL中的保留字。
mycommand = New SqlCommand("INSERT INTO tblTeacherInfo [(Date)] values ('" & txtDate.Text & "')", con)