此代码有问题 [英] problem with this code
本文介绍了此代码有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经使用此代码验证了不允许重复值的商品,但是我对此有疑问
i have validate an item which does not allow duplicate value by using this code but i have problem on it
If SQLstr <> Nothing Then
MessageBox.Show("name is already used ")
End If
这是我的源代码:
this is my source code:
Try
Dim curdate As String
Dim recstatus As String
curdate = Now.Date
recstatus = "A"
If txtuser.Text = "" Then
MessageBox.Show("Blank Record is NOT accepted")
Exit Sub
End If
If txtpass.Text = "" Then
MessageBox.Show("Blank Record is NOT accepted")
Exit Sub
End If
If txtcpass.Text = "" Then
MessageBox.Show("Blank Record is NOT accepted")
Exit Sub
End If
If txtpass.Text.Trim <> txtcpass.Text.Trim Then
MessageBox.Show("Both Password and Confirm Password Not Match")
Exit Sub
End If
If (Comlevel.SelectedIndex = -1) Or Comlevel.Text = "" Then
MessageBox.Show("Please select Level")
Exit Sub
End If
Mycn = New SqlConnection("Data Source=IMAN-PC;Initial Catalog=control car;Integrated Security=True")
Mycn.Open()
SQLstr = "insert into login values('" & recstatus & "','" & txtuser.Text & "','" & txtpass.Text & "','" & Comlevel.Text & "','" & usrID & "','" & curdate & "','" & usrID & "', '" & curdate & "')"
command = New SqlCommand(SQLstr, Mycn)
icount = command.ExecuteNonQuery
If SQLstr <> Nothing Then
MessageBox.Show("name is here")
End If
If icount = 1 Then
MessageBox.Show("Record Added Successfully", "Add Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("No Record Inserted", "Add Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Mycn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message & "Could Not Insert Record")
'Mycn.Close()
推荐答案
您可以使用SQL
子句EXIST
避免插入重复的行,例如,参见FAQ ^ ]
You may use theSQL
clauseEXIST
to avoid inserting duplicate rows, see, for instance, the first question in the FAQ here[^].
您必须检查数据库中是否已存在用户ID.请参考以下代码:
You Have to Check that the User ID is Already Exist Or Not in DataBase.Refer the following Code :
Dim _Result As Object
Dim _ObjCmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand
_ObjCmd.CommandText = "Select * From Login Where UserID ='" & txtUser.Text & "'"
_ObjCmd.Connection = _SQLConnection
Try
_SQLConnection.Open()
_Result = _ObjCmd.ExecuteReader
_SQLConnection.Close()
Catch ex As Exception
End Try
If _Result Is DBNull.Value Then
'Your Insert Code
Else
MessageBox.Show("User Id Already Exist.")
End If
希望对您有帮助.:)
I hope it will help you.:)
您的变量SQLstr
永远不会等于零,因为在此之前将变量设置为插入SQL的三行.所以你的if语句...
Your variableSQLstr
is never going to equal nothing because you set the variable to your insert SQL three lines before that. So your if statement...
If SQLstr <> Nothing Then
MessageBox.Show("name is here")
End If
总是会被点击,因为SQLstr
总是将您在其中设置的文本放在三行之前.
检查数据库中是否已经有一个值不是正确的值.我同意Manoj K Bhoir和CPallini的观点,您需要分别进行检查或通过其他方式进行检查.
will ALWAYS be hit because SQLstr
will ALWAYS have the text you set in it three lines before.
It''s not the correct value to check to see if you already have a value in your database. I agree with Manoj K Bhoir and CPallini that you need to perform the check separately and by other means.
这篇关于此代码有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文