【问题标题】:VB Win Form Issue Using SqlCommand使用 SqlCommand 的 VB Win 表单问题
【发布时间】:2020-03-17 10:31:58
【问题描述】:

我目前正在 Visual Studio 2019 中创建一个基于 win 表单的工具,该工具从 SQL 数据库中读取数据。从我正在处理的表单中将字段拉入 sqlCommand 查询时,我遇到了困难。

这是我的 VB 脚本的一部分。请注意,如果没有类似姓氏的过滤器,这可以正常工作,如果我用硬编码的姓氏编写它,例如“亚当斯”,它也可以工作。我也可以直接在 SQL 中使用变量来获得相同的逻辑。消息弹出窗口按预期显示“Adams”,但数据网格中未返回任何内容。

下面是根据需要运行表单以及硬编码为“Adams”的结果的屏幕截图。

提前感谢您的帮助:)

Public Class Form4
Public Sub BtnFetchAdastraUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFetchAdastraUser.Click

    Dim connetionString As String = "Data Source=xxxxxx;Initial Catalog=xxxxxx;User ID=xxxxx;Password=xxxxx"
    Dim dt As New DataTable()

    Using connection As New SqlConnection(connetionString)
        Dim command As New SqlCommand(
                                        "select 
                                            [u].[UserRef],
                                            [u].[UserName],
                                            [u].[FullName]
                                        from dbo.[Users][u]
                                        where
                                            [Obsolete] = 0
                                                and [Surname] like '%" + Surname.Text + "%'", connection
                                      )
        command.Connection.Open()
        Dim sqlAdaptr As New SqlDataAdapter(command)
        Dim ds As New DataTable
        sqlAdaptr.Fill(ds)
        DataGridView1.DataSource = ds

        MsgBox(Surname.Text)

        command.Connection.Close()
    End Using
End Sub

End Class

【问题讨论】:

    标签: sql vb.net visual-studio-2019 sqlcommand


    【解决方案1】:

    我想通了。我通过我的 MsgBox 运行 command.CommandText,它突出显示了我在查询中传递的以下问题!

    我在我正在通过的字段的前面添加了文本。

    【讨论】:

      【解决方案2】:

      建议您使用参数编写SQL查询语句。 可以改成如下:

          Using connection As New SqlConnection(connetionString)
              connection.Open()
              Dim command As New SqlCommand(
                                              "select 
                                              [u].[UserRef],
                                              [u].[UserName],
                                              [u].[FullName]
                                          from dbo.[Users][u]
                                          where
                                              [Obsolete] = 0
                                                  and [Surname] like @Surname", connection
                                            )
              command.Parameters.Clear()
              command.Parameters.AddWithValue("@Surname", "%" & Surname.Text & "%")
      
              Dim sqlAdaptr As New SqlDataAdapter(command)
              Dim ds As New DataTable
              sqlAdaptr.Fill(ds)
              DataGridView1.DataSource = ds
      
              MsgBox(Surname.Text)
              MsgBox(command.CommandText)
      
              command.Connection.Close()
          End Using
      

      【讨论】:

      • 使用参数:不仅推荐而且强制实践。使此代码崩溃所需的只是像 O'connor 这样的姓氏。此外,应该对来自文本框的输入进行清理,使用修剪功能消除可能的空白。在进行复制粘贴时,多余的空格、制表符、回车会经常潜入。
      • 是的,Surname.Text 可以是任何东西,如果其中包含 ',它可能会破坏查询。
      猜你喜欢
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多