所以我有一个能够为名为CustomerID的用户生成和ID的函数。这是我现在正在使用的功能
Private Function GenerateID() As String
SqlCon = New SqlConnection(conString)
Dim value As String = "0000"
Try
SqlCon.Open()
SqlCmd = New SqlCommand("SELECT TOP 1 ID FROM Customer ORDER BY ID DESC", SqlCon)
SqlDR = SqlCmd.ExecuteReader(CommandBehavior.CloseConnection)
If SqlDR.HasRows Then
SqlDR.Read()
value = SqlDR.Item("ID")
End If
SqlDR.Close()
value += 1
If value <= 9 Then
value = "000" & value
ElseIf value <= 99 Then
value = "00" & value
ElseIf value <= 999 Then
value = "0" & value
End If
Catch ex As Exception
If SqlCon.State = ConnectionState.Open Then
SqlCon.Close()
End If
End Try
Return value
End Function
在其他方法中我会这样称呼它:
Sub auto()
Try
txtID.Text = GenerateID()
txtClienteID.Text = "C-" + GenerateID()
Catch ex As Exception
MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
我发现问题来自这一行:txtClienteID.Text = "C-" + GenerateID()
,如果我删除像这样txtClienteID.Text = "C-"
的GenerateID(),它会给我错误。现在,如果我删除 - 并执行此操作txtClienteID.Text = "C"
它将实际工作。
始终显示的错误是
字符串或二进制数据将被截断
CustomerID数据库的数据类型为nvarchar(50)
那你对此有什么想法吗?