【问题标题】:How to get a line in SQL Database Table to Print in Textboxes?如何在 SQL 数据库表中获取一行以在文本框中打印?
【发布时间】: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


【解决方案1】:

我使用了 convert.tostring(reader.getsqlstring(#)) 并且成功了

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 = Convert.ToString(reader.GetSqlString(0))
                TextBox7.Text = Convert.ToString(reader.GetSqlString(1))
                TextBox8.Text = Convert.ToString(reader.GetSqlString(2))
                TextBox9.Text = Convert.ToString(reader.GetSqlString(3))
                TextBox10.Text = Convert.ToString(reader.GetSqlString(4))
                TextBox11.Text = Convert.ToString(reader.GetSqlString(5)).Substring(0, 3)
                TextBox12.Text = Convert.ToString(reader.GetSqlString(5)).Substring(4, 3)
                TextBox13.Text = Convert.ToString(reader.GetSqlString(5)).Substring(8, 4)
            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

【讨论】:

    猜你喜欢
    • 2018-02-24
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    相关资源
    最近更新 更多