【发布时间】: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