【发布时间】:2015-06-15 18:44:40
【问题描述】:
我有一个“销售人员”表,其中包含名字、姓氏、地址、城市、州和电话号码的参数。
我在此数据库中搜索单个条目,并且希望将 1 个条目的结果打印在其各自的文本框中以供用户编辑。我不断收到 SQL 异常,我不确定现在使用哪个代码。
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim myconnection As New SqlConnection("server=classified;database=classified")
myconnection.Open()
Dim thePhoneQuery As String = "SELECT * FROM Salespeople WHERE [First Name]=@FirstName AND [Last Name]=@LastName AND [Phone Number]=@PhoneNumber"
Dim phoneValidator As SqlCommand = New SqlCommand(thePhoneQuery, myconnection)
BindGridSelectedSalespeople()
phoneValidator.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text)
phoneValidator.Parameters.Add("@LastName", SqlDbType.VarChar).Value = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(TextBox2.Text)
phoneValidator.Parameters.Add("@PhoneNumber", SqlDbType.VarChar).Value = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(TextBox3.Text + "-" + TextBox4.Text + "-" + TextBox5.Text)
Using reader As SqlDataReader = phoneValidator.ExecuteReader()
If reader.HasRows Then
' User already exists
MsgBox("This salesperson already exists in the database.", MsgBoxStyle.Exclamation)
Label8.Visible = True
Label9.Visible = True
Label10.Visible = True
Label11.Visible = True
Label12.Visible = True
Label13.Visible = True
Label14.Visible = True
Label15.Visible = True
TextBox6.Visible = True
TextBox7.Visible = True
TextBox8.Visible = True
TextBox9.Visible = True
TextBox10.Visible = True
TextBox11.Visible = True
TextBox12.Visible = True
TextBox13.Visible = True
While (reader.Read())
TextBox6.Text = reader.Read
TextBox7.Text = (reader("@LastName"))
TextBox8.Text = (reader("@Address"))
TextBox9.Text = (reader("@City"))
TextBox10.Text = (reader("@State"))
End While
Else
reader.Close()
MsgBox("The Salesperson '" + TextBox1.Text + "' '" + TextBox2.Text + "' does not exist. Please check the name and try again, or add them as a new salesperson.", MsgBoxStyle.Exclamation)
End If
End Using
myconnection.Close()
End Sub
【问题讨论】:
-
您遇到了什么 SQL 异常?
-
不要给
reader.Read打电话两次。 -
阅读器中的字符串 -
reader("Lastname")不应包含@符号。 -
假设我在其中保留了 While (reader.Read()) 语句,我应该为每个文本框分配放置什么? IE。当我输入 ("[First Name]").ToString() 时,我确实得到了 "[First Name]" 打印。当我把 read 放在它前面时,我得到一个未处理的 SQL 异常。
-
我使用了 convert.tostring(reader.getsqlstring(#)) 并且成功了
标签: sql sql-server vb.net