SqlCommand cmdConfirmExist = new SqlCommand( "Select * from Employees where ID = "+ txtUID.Text.Trim() + "", conn );
conn,Open();
if( cmdConfirmExist.ExecuteNonQuery() >=1 )
{
Label1.Text = "<Script language = JavaScript > alert(The user ID is already exist!); </Script>";
}
else
{
//insert the new record
}
每次在Page_Load()中重置 Label1.Text为""
现在的情况是无论数据库中有没有重复的ID,都弹出对话框"The user ID is already exist!"
Dim Conn As New OleDbConnection("Data Source=""" & Me.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings("database")) & """;Provider=""Microsoft.Jet.OLEDB.4.0"";User ID=Admin")
Dim commstr As String
commstr = "select username from T_UserInfo where username =" & Me.TextBox1.Text & ""
Dim mycomm As New OleDbCommand(commstr, Conn)
Conn.Open()
Dim dr As OleDbDataReader = mycomm.ExecuteReader
If dr.Read Then
Me.Label3.Text = "对不起," & Me.TextBox1.Text & "已经被占用!"
dr.Close()
Else
Me.Label3.Text = "恭喜您," & Me.TextBox1.Text & "可以正常注册!"
End If
Conn.Close()
9个月前写的 ^_^
ExecuteNonQuery()的返回值不能用于select命令,只对UPDATE, INSERT, and DELETE 有意义
可以这样写
SqlCommand cmdConfirmExist = new SqlCommand( "Select count(*) from Employees where ID = "+ txtUID.Text.Trim() + "", conn );
conn.Open();
if( (int)cmdConfirmExist.ExecuteScalar() >=1 )
另外这样拼sql语句很危险,会被注入的