Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在VB.NET上检测重复条目?_Sql Server_Vb.net_Duplicates - Fatal编程技术网

Sql server 如何在VB.NET上检测重复条目?

Sql server 如何在VB.NET上检测重复条目?,sql-server,vb.net,duplicates,Sql Server,Vb.net,Duplicates,我在使用SQL Server在VB.NET中避免或检测重复数据条目时遇到问题,并且在出现重复条目时会弹出消息 这是我的添加按钮代码: Dim sql=插入studentAttendencestudentID、日期、时间、主题、出席人数& VALUES@studentID,@日期,@时间,@主题,@出席& 其中不存在从studentAttendence中选择*,其中studentID=@studentID 使用cn作为新的SqlConnectionMy sql连接字符串 Dim命令作为SqlCom

我在使用SQL Server在VB.NET中避免或检测重复数据条目时遇到问题,并且在出现重复条目时会弹出消息

这是我的添加按钮代码:

Dim sql=插入studentAttendencestudentID、日期、时间、主题、出席人数& VALUES@studentID,@日期,@时间,@主题,@出席& 其中不存在从studentAttendence中选择*,其中studentID=@studentID 使用cn作为新的SqlConnectionMy sql连接字符串 Dim命令作为SqlCommand=newsqlcommandsql,cn 命令。参数。Add@studentID,SqlDbType.NVarChar.Value=TextBox1.Text 命令。参数。Add@date,SqlDbType.NVarChar.Value=DateTimePicker1.Text 命令。参数。Add@time,SqlDbType.NVarChar.Value=DateTimePicker2.Text 命令。参数。Add@subject,SqlDbType.NVarChar.Value=ComboBox1.Text 命令。参数。Add@attendence,SqlDbType.NVarChar.Value=ComboBox2.Text commands.ExecuteNonQuery 终端使用
或者是否有其他方法来避免重复条目。我只是想避免添加相同日期和主题的学生。因此,如果我已经在今天的studentID日期添加了主题,那么我只能在下一个日期添加主题。

您应该创建一个函数,查询数据表以返回日期、主题和studentID与要插入的内容相匹配的行数。如果返回的结果大于0,则显示消息

下面是一个函数,如果出现错误或行计数错误,该函数将返回-1:

私有函数GetRowCountByVal studentIdParameter作为字符串,ByVal dateParameter作为DateTime,ByVal subjectParameter作为字符串作为整数 '声明要返回的对象 整数形式的Dim计数=-1 '声明连接对象 Dim con作为OLEDB连接 '在Try/Catch中包装代码 尝试 '将连接对象设置为新实例 'TODO:在此处使用有效的连接字符串更改我的连接字符串 con=新建OleDbConnectionMy连接字符串 '创建命令对象的新实例 'TODO:将[ID]更改为有效列 使用cmd作为OleDbCommand=New OleDbCommand从[MyTable]中选择Count[ID],其中studentID=@studentID,date=@date,subject=@subject,con '参数化查询 cmd.Parameters。Add@studentId,OleDbType.NVarChar.Value=学生参数 cmd.Parameters。Add@date,OleDbType.DBDate.Value=dateParameter.Date cmd.Parameters。Add@subject,OleDbType.NVarChar.Value=subjectParameter '打开连接 未结 '使用ExecuteScalar返回单个值 计数=Convert.ToInt32cmd.ExecuteScalar '关闭连接 结案 终端使用 特例 '显示错误 Console.WriteLineex.Message 最后 '检查连接对象是否已初始化 如果con不是什么,那么 如果con.State=ConnectionState.Open,则 '如果抛出的是left openexception,请关闭连接 结案 如果结束 '处理连接对象 违例处置 如果结束 结束尝试 '返回行计数 返回计数 端函数 要实现它,您可以执行以下操作:

Dim rowCount=GetRowCountTextBox1.Text,DateTimePicker1.Value,ComboBox1.Text 如果rowCount=-1,则 MessageBox.show检查重复项时出错 ElseIf rowCount=0,则 '插入记录 其他的 MessageBox.show已存在具有此studentId、日期和主题的记录。 如果结束
如果插入之前不存在,则可以使用。此方法只需要对数据库进行一次点击。在Using块的第一行末尾添加一个逗号,然后在命令之前删除Dim。这将包括使用块中的命令

Private Function InsertAttendance() As Integer
    Dim sql = "If Not Exists (Select 1 From studentAttendence 
Where studnetID = @studentID And date = @date And subject = @subject)
INSERT INTO studentAttendence(studentID, date, time, subject, attendence) 
VALUES(@studentID, @date, @time, @subject, @attendence);"
    Dim NumRowsAffected As Integer
    Using cn As New SqlConnection("My sql Connection string"),
           commands As SqlCommand = New SqlCommand(sql, cn)
        commands.Parameters.Add("@studentID", SqlDbType.NVarChar).Value = TextBox1.Text
        commands.Parameters.Add("@date", SqlDbType.NVarChar).Value = DateTimePicker1.Text
        commands.Parameters.Add("@time", SqlDbType.NVarChar).Value = DateTimePicker2.Text
        commands.Parameters.Add("@subject", SqlDbType.NVarChar).Value = ComboBox1.Text
        commands.Parameters.Add("@attendence", SqlDbType.NVarChar).Value = ComboBox2.Text
        cn.Open()
        NumRowsAffected = commands.ExecuteNonQuery()
    End Using
    Return NumRowsAffected
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        If InsertAttendance() = 1 Then
            MessageBox.Show("Success")
        Else
            MessageBox.Show("Duplicate Entry")
        End If
    Catch ex As Exception
        MessageBox.Show($"Error Entering Attendance {ex.Message}")
    End Try
End Sub

确保在数据库中检查参数的数据类型

您没有定义什么是重复数据,也没有努力获取重复数据。你能澄清一下吗?@David我想防止添加重复数据。例如,今天的日期我在学生id上添加了一个主题。因此,我必须避免在学生id上的今天添加相同的主题,但我可以在学生id的今天添加不同的主题,这正是我想要的。你能清楚地定义什么是复制品吗?只是日期、学生id和科目无法与现有记录匹配,还是有其他要求?@David只是日期、科目和学生id无法与现有记录匹配。为什么要将日期存储为NVarChar?日期应存储为日期。它只是不断地告诉您,检查重复项时出错,我也无法输入任何新值。如果您将连接添加到Using块,则可以删除Finally。无需检查连接。St
在关闭连接之前进行ate。从MS文档中,应用程序可以多次调用Close。没有生成异常。OP似乎正在使用Sql Server。看到OleDb代码可能会混淆OP。对不起,我不知道为什么我认为你在使用OleDb。您只需要将其更改为使用适当的类。但现在我只是将异常打印到控制台,所以请检查那里的异常消息或将其更改为使用MessageBox。我收到一条错误消息,在关键字WHERE附近显示不正确的语法。所有的数据类型都完全改变了。很抱歉。我忘了去掉where子句中不存在的。查看已更正的Dim sql字符串。非常感谢,我很高兴了解复制条目的新方法,它帮助了我很多,问题也解决了: