gpt4 book ai didi

sql - DataAdapter.Fill错误

转载 作者:行者123 更新时间:2023-12-03 08:31:30 24 4
gpt4 key购买 nike

我从HERE复制了一些代码,但出现错误:“')'附近的语法不正确。
但是我看不到这段代码有什么问题

Dim conString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\users.mdf;Integrated Security=True;User Instance=True"
Dim con As New SqlConnection(conString)
con.Open()
Try
Dim dataAdapter1 As New SqlDataAdapter( _
New SqlCommand("SELECT subject, info, username, status FROM(problems) WHERE (username =" & userName & ")", con))
Dim ds As New DataSet("DataSetMyProbs")
ds.Tables.Add("UserProbs")
dataAdapter1.Fill(ds.Tables("UserProbs")) '//This is where i get the error'
Me.bsMyProblems.DataSource = ds.Tables("UserProbs")
Dim dataAdapter2 As New SqlDataAdapter( _
New SqlCommand("SELECT dep, pcid, username, status, extraInfo FROM(deployments) WHERE (username = " & userName & ")", con))
ds.Tables.Add("UsersDepl")
dataAdapter2.Fill(ds.Tables("UserDepl"))
Me.bsMyDepl.DataSource = ds.Tables("UserDepl")
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
con.Dispose()
End Try

最佳答案

我认为可能是因为您的查询,您的参数userName是字符串吗?您可能需要为字符串加上单引号,并在“FROM”和表名之间留一个空格

Dim dataAdapter1 As New SqlDataAdapter( _
New SqlCommand("SELECT subject, info, username, status FROM [problems] WHERE (username ='" & userName & "')", con))

关于sql - DataAdapter.Fill错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10491477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com