简体   繁体   English

使用MS SQL作为数据库的VB.net程序。 然后,当我尝试向表中插入另一个值时弹出此错误

[英]Working on a VB.net program with MS SQL as my database. Then this error popped-up when I'm trying to insert another value into my table

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

Here are my codes, and I'm very lost to what I'm going to do next. 这是我的代码,我对接下来要做的事情非常迷茫。 Thanks in advance for the help. 先谢谢您的帮助。

Just Dispose you sqlCommand every after using it. 只需在每次使用后处置sqlCommand。
you can use: 您可以使用:

Using c As New SqlCommand("commandtext", connection")

Or you can just.. 或者你可以..

With c
'after the execution
.Parameters.Clear()
.Dispose
End With

Create a new sqlcommand every time. 每次都创建一个新的sqlcommand。 The same way you create a new Connection every time. 每次创建新连接的方式相同。

The problem you have is that you're adding @name again on the second pass. 您遇到的问题是您在第二遍再次添加了@name。

Using con As New SqlConnection(connection) 
    Using cmd as New SqlCommand(...)

    ....

    End Using
End Using

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试将vb.net项目运行到连接到SQL Server 2008的另一台笔记本电脑上 - I'm trying to run my vb.net project to another laptop connected to SQL Server 2008 出现SQL Server错误:“已经有一个名为&#39; <my table> 在数据库中。 “表不在数据库中时” - Getting SQL Server error: “There is already an object named '<my table>' in the database. ” when table is NOT in database 如何从数据表vb.net向sql server数据库插入值 - How to Insert value to sql server database from data table vb.net 无法通过我的vb.net程序连接到SQL Server - Can't connect to SQL Server with my vb.net program 使用 VB.net 还原 SQL 数据库时出错 - Getting Error when Restoring SQL Database using VB.net 如何在SQL中为我的vb.net应用程序使用动态数据库名称? - How can I use a dynamic database name in SQL for my vb.net app? 我试图掌握创建使用SQL Server数据库的程序的概念,但是我习惯于仅在本地计算机上运行它 - I'm trying to grasp the concept of creating a program that uses a SQL Server database, but I'm used to running it only on my local machine 在VB.Net上将数据库表数据显示到ListView时出错 - Error when displaying database table data to listview on VB.Net 从vb.net程序内部比较2个SQL Server数据库 - Compare 2 SQL server database from inside a vb.net program 使用vb.net将数据插入SQL Server数据库 - Insert data into a SQL Server database using vb.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM