select Distinct 这个语法难倒了“reader = mycomm.ExecuteReader”这句么?为什么不能执行
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New SqlConnection
Dim mycomm As New SqlCommand
If TextBox2.Text = " " Or TextBox3.Text = " " Or TextBox4.Text = " " Then
MsgBox( "警告:请填写完整信息(第一个文本框可以不填) ", MsgBoxStyle.OkOnly)
Response.Redirect( "tea.aspx ")
lblmsg.Text = "......出错了 "
End If
Dim reader As SqlDataReader
conn.ConnectionString = "data source=(local);uid=sa;pwd=;database=kejian; "
conn.Open()
mycomm.Connection = conn
mycomm.CommandText = "select Distinct 教师名,密码 from teacher where 教师名= ' " & Trim(TextBox2.Text) & " ' and 密码= ' " & Trim(TextBox3.Text) & " ' "
reader = mycomm.ExecuteReader
If reader.Read = True Then
MsgBox( "不允许添加这样巧合的记录 ")
’我是想在textbox2(对应 "教师名 "字段和textbox3(对应 "密码 "字段)中如果输入了在teacher表中已经有如a(教师名),b(密码)这样的记录(同1个记录中)组合,那么就返回错误,即想表达这句:MsgBox( "不允许添加这样巧合的记录 "),可是没能如愿
Response.Redirect( "tea.aspx ")
lblmsg.Text = "......出错了 "
Else
mycomm = New SqlCommand( "Insert into [teacher] (老师名,密码,所属院系) values( ' " & Trim(TextBox2.Text) & " ', ' " & Trim(TextBox3.Text) & " ', ' " & Trim(TextBox4.Text) & " ' ) ", conn)
mycomm.ExecuteNonQuery()
conn.Close()
Response.Redirect( "tea.aspx ")
lblmsg.Text = "......添加成功 "
End If
End Sub
[解决办法]
试一下用 select COUNT * from teacher where 教师名= ' " & Trim(TextBox2.Text) & " ' and 密码= ' " & Trim(TextBox3.Text) & " ' "
若值为 0 则允许新增.