Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Main
Dim c As New SqlCommand
Dim connection As String = "Server=DESKTOP-7MC233A\SQLJOSE;database=AttendanceLog;user id=sa;password=pogi1234;"
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Dim dtmNow As DateTime = System.DateTime.Now
If dtmNow.Hour > 0 And dtmNow.Hour < 11 Then
Label2.Text = "Good morning"
ElseIf dtmNow.Hour > 12 And dtmNow.Hour < 18 Then
Label2.Text = "Good afternoon"
ElseIf dtmNow.Hour > 19 And dtmNow.Hour < 23 Then
Label2.Text = "Good evening "
Else
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
lblDate.Text = Date.Now.ToString("MMM dd,yyyy")
lblTime.Text = Date.Now.ToString("hh:mm:ss")
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles lblDate.Click
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbNames.SelectedIndexChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using con As New SqlConnection(connection)
Try
If con.State = ConnectionState.Closed Then
con.Open()
End If
With c
.Connection = con
.CommandText = "Insert into TimeIn values(@Name,@TimeIn,@Date)"
.CommandType = CommandType.Text
.Parameters.AddWithValue("@Name", cmbNames.Text)
.Parameters.AddWithValue("@TimeIn", lblTime.Text)
.Parameters.AddWithValue("@Date", lblDate.Text)
.ExecuteNonQuery()
End With
Dim dtmNow As DateTime = System.DateTime.Now
If dtmNow.Hour > 9 Then
MsgBox("You are late!!!", MsgBoxStyle.Critical)
ElseIf dtmNow.Hour < 9 Then
MsgBox("You are on time!", MsgBoxStyle.Information)
End If
Catch ex As Exception
MsgBox("From: " & ex.Message)
End Try
con.Dispose()
End Using
End Sub
End Class
以下是我的代码,我很遗憾接下来要做的事情。在此先感谢您的帮助。
答案 0 :(得分:1)
使用它后,每次使用sqlCommand 你可以使用:
Using c As New SqlCommand("commandtext", connection")
或者你可以......
With c
'after the execution
.Parameters.Clear()
.Dispose
End With
答案 1 :(得分:0)
每次都创建一个新的sqlcommand。与每次创建新Connection的方式相同。
你遇到的问题是你在第二次传球时再次添加@name。
Using con As New SqlConnection(connection)
Using cmd as New SqlCommand(...)
....
End Using
End Using