.net 在数据库中存储空数据的验证代码
我用VB.NET编写了一个简单的验证代码。此代码正在数据库表中存储空数据。如何避免这种情况,以及在.net 在数据库中存储空数据的验证代码,.net,vb.net,.net,Vb.net,我用VB.NET编写了一个简单的验证代码。此代码正在数据库表中存储空数据。如何避免这种情况,以及在else if语句之后编写什么代码?i、 e电话后没有验证 代码如下: Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click If Len(Trim(txtName.Text)) = 0 Then MsgBox("Enter Nam
else if
语句之后编写什么代码?i、 e电话后没有验证
代码如下:
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
If Len(Trim(txtName.Text)) = 0 Then
MsgBox("Enter Name", MsgBoxStyle.Critical, "Error")
txtName.Focus()
ElseIf Len(Trim(txtAge.Text)) = 0 Then
MsgBox("Enter Age", MsgBoxStyle.Critical, "Error")
txtAge.Focus()
ElseIf Len(Trim(txtPhone.Text)) = 0 Then
MsgBox("Enter Phone", MsgBoxStyle.Critical, "Error")
txtPhone.Focus()
Else
Dim blnFlag As Boolean = False
MsgBox("Enter the details", MsgBoxStyle.Critical, "Error")
End If
Try
Dim strCommand As String
strCommand = "Insert into [Validation] ([Name],[Age],[Phone]) VALUES"
strCommand = strCommand & "('" & Trim(txtName.Text) & "','" & Trim(txtAge.Text) & "','" & Trim(txtPhone.Text) & "')"
Dim StrConnection As String
StrConnection = ConfigurationManager.ConnectionStrings("ConnectionString").ToString
Dim cnValidation As New SqlClient.SqlConnection(StrConnection)
If (cnValidation.State = ConnectionState.Closed) Then
cnValidation.Open()
End If
Dim cmdEmployee As New SqlClient.SqlCommand(strCommand, cnValidation)
cmdEmployee.ExecuteNonQuery()
cnValidation.Close()
MsgBox("Save Successful", MsgBoxStyle.Information, "Success")
Catch ex As Exception
MsgBox("Save failed " & ex.Message, MsgBoxStyle.Critical, "Failed")
End Try
最简单的方法是,在验证失败后从方法返回:
If Len(Trim(txtName.Text)) = 0 Then
MsgBox("Enter Name", MsgBoxStyle.Critical, "Error")
txtName.Focus()
Return
ElseIf Len(Trim(txtAge.Text)) = 0 Then
MsgBox("Enter Age", MsgBoxStyle.Critical, "Error")
txtAge.Focus()
Return
ElseIf Len(Trim(txtPhone.Text)) = 0 Then
MsgBox("Enter Phone", MsgBoxStyle.Critical, "Error")
txtPhone.Focus()
Return
Else
Dim blnFlag As Boolean = False
MsgBox("Enter the details", MsgBoxStyle.Critical, "Error")
Return
End If
但是,“输入详细信息”
部分不清楚。为什么总是显示错误消息框?似乎有一个合乎逻辑的问题。从今以后,我将忽略这一部分
下面是一个更可读的.NET版本的代码:
Dim validName = Not String.IsNullOrWhiteSpace(txtName.Text)
Dim validAge = Not String.IsNullOrWhiteSpace(txtAge.Text)
Dim validPhone = Not String.IsNullOrWhiteSpace(txtPhone.Text)
Dim isValid = validName AndAlso validAge AndAlso validPhone
If Not isValid Then
If Not validName Then
MsgBox("Enter Name", MsgBoxStyle.Critical, "Error")
txtName.Focus()
ElseIf Not validAge Then
MsgBox("Enter Age", MsgBoxStyle.Critical, "Error")
txtAge.Focus()
ElseIf Not validPhone Then
MsgBox("Enter Phone", MsgBoxStyle.Critical, "Error")
txtPhone.Focus()
End If
Return ' return from this method '
Else
' insert into DB '
' .... '
End If
旁注:即使这是一个windows应用程序,也应该真正使用sql参数。它不仅可以防止您出现以下问题,还可以防止本地化问题(例如使用
datetime
):
您应该对所有实现使用Using
-语句IDisposable
,它会处理非托管资源,即使出现错误,它也会关闭连接
我还展示了如何从SQLServer中的identity
列确定新创建的标识值
注意,我已经将
txtAge.Text
解析为Int32
,因为我假设数据库中的列类型实际上是int
。如果不是这样,请删除Int32.Parse
。通常,您应该始终提供正确的类型作为参数。不确定为什么需要最后一个Else,最好在命令中使用SqlParameter
。是的,每当代码进入If或ElseIf时,您需要从方法返回。@AdarshShah:或使用我的第二种方法;)是的,但我唯一的问题是,如果将来有人在else块之后添加代码,那么如果验证失败,它仍然会被调用。我认为第1种方法更易于维护。@AdarshShah:注意了,编辑了我的答案,在Not isValid
-块中添加了Return
语句。但请注意,如果OP希望在Else
块之后执行代码,即使验证失败,这也可能是不正确的。唯一不应该执行的部分是由If/Else
确保的插入。
Try
Dim newIdenity As Int32 ' determine new ID generated from database '
Dim strCommand = "Insert into [Validation] ([Name],[Age],[Phone]) VALUES (@Name,@Age,@Phone)"
Dim StrConnection As String = ConfigurationManager.ConnectionStrings("ConnectionString").ToString
Using cnValidation = New SqlClient.SqlConnection(StrConnection)
Using cmdEmployee = New SqlCommand(strCommand, cnValidation)
cnValidation.Open()
cmdEmployee.Parameters.AddWithValue("@Name",txtName.Text)
cmdEmployee.Parameters.AddWithValue("@Age",Int32.Parse(txtAge.Text))
cmdEmployee.Parameters.AddWithValue("@Phone",txtPhone.Text)
newIdenity = DirectCast(cmdEmployee.ExecuteScalar(), Int32)
End Using
End Using
MsgBox("Save Successful", MsgBoxStyle.Information, "Success")
Catch ex As Exception
MsgBox("Save failed " & ex.Message, MsgBoxStyle.Critical, "Failed")
End Try