目标,判断是否主键重复,不重复则登录新数据,重复则不登录。
界说范例:
DataRecord
tableName 表名
rowNumber 行号
columnName 列名
data 数据
想要实现的代码逻辑如下:
模拟数据库的登录过程。假设现在登录了5条数据,要登录第6条数据。
规则1,判断其他5行数据的全部列,与第6行已经录的全部列的数据逐一比力,如果存在差别等的,分析主键不重复,可以登录新的数据。
规则2,如果不存在同等的,则判断我们这次要登录的这个列的其他行的数据,是否存在差别等的,如果差别等则主键不重复,可以登录新的数据。
规则3,如果仍然没有差别等的数据,分析主键重复,不能登录这条数据。
代码布局如下:
私有DataRecordType类
模块1
DataRecordType类代码- ' DataRecordType 类模块
- Private pTableName As String
- Private pRowNumber As Long
- Private pColumnName As String
- Private pData As String
- ' 定义公共属性以访问私有变量
- Public Property Get tableName() As String
- tableName = pTableName
- End Property
- Public Property Let tableName(value As String)
- pTableName = value
- End Property
- Public Property Get rowNumber() As Long
- rowNumber = pRowNumber
- End Property
- Public Property Let rowNumber(value As Long)
- pRowNumber = value
- End Property
- Public Property Get columnName() As String
- columnName = pColumnName
- End Property
- Public Property Let columnName(value As String)
- pColumnName = value
- End Property
- Public Property Get data() As String
- data = pData
- End Property
- Public Property Let data(value As String)
- pData = value
- End Property
复制代码 模块1代码- Sub AddData(dataCollection As Collection, tableName As String, rowNumber As Long, columnName As String, data As String)
- Dim record As DataRecordType
- Set record = New DataRecordType ' 关键:实例化对象
- record.tableName = tableName
- record.rowNumber = rowNumber
- record.columnName = columnName
- record.data = data
- dataCollection.Add record
- End Sub
- Function InsertData(dataCollection As Collection, tableName As String, rowNumber As Long, columnName As String, data As String) As Boolean
- Dim i As Long
- Dim sameTableRowData As String
- Dim otherTableRowData As String
- Dim record1 As DataRecordType
- Dim record2 As DataRecordType
-
- Dim noexist As Boolean
-
- For i = 1 To dataCollection.Count
- Set record1 = dataCollection(i) ' 关键:从集合中获取对象
- If record1.tableName = tableName And record1.rowNumber = rowNumber Then
- For j = 1 To dataCollection.Count
- Set record2 = dataCollection(j)
- If record2.tableName = record1.tableName And record2.columnName = record1.columnName And record2.rowNumber <> record1.rowNumber Then
- If record2.data <> record1.data Then
- Call AddData(dataCollection, tableName, rowNumber, columnName, data)
- InsertData = True
- Exit Function
- End If
- Exit For
- End If
- Next j
- End If
- Next i
-
- For j = 1 To dataCollection.Count
- Set record2 = dataCollection(j)
- If record2.tableName = tableName And record2.columnName = columnName And record2.rowNumber <> rowNumber Then
- If record2.data <> data Then
- Call AddData(dataCollection, tableName, rowNumber, columnName, data)
- InsertData = True
- Exit Function
- End If
- Exit For
- End If
- Next j
-
- InsertData = False
- End Function
- Sub TestInsertData()
- Dim dataCollection As Collection
- Set dataCollection = New Collection
-
- ' 添加前 5 条数据
- Call AddData(dataCollection, "Table1", 1, "Column1", "Data1")
- Call AddData(dataCollection, "Table1", 1, "Column2", "Data2")
- Call AddData(dataCollection, "Table1", 1, "Column3", "Data3")
- Call AddData(dataCollection, "Table1", 2, "Column1", "Data1")
- Call AddData(dataCollection, "Table1", 2, "Column3", "Data3")
-
- ' 插入第 6 条数据并检查结果
- Dim result As Boolean
- result = InsertData(dataCollection, "Table1", 2, "Column2", "Data2")
-
- If result Then
- MsgBox "第 6 条数据已插入。"
- Else
- MsgBox "第 6 条数据未插入(与现有数据重复)。"
- End If
- End Sub
复制代码 运行TestInsertData()

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
|