正如标题所说,我有一个数据库和一个表正在尝试连接到使用vb.net,如果我使用vb.net运行select它会说无效的对象名称' [表名] 但SSMS中粘贴的相同SQL查询副本运行正常,我对它疯狂。任何帮助将非常感谢,提前感谢 这是代码
Public Sub importVehicles()
Dim con As New SqlConnection()
Dim configs = ConfigurationManager.AppSettings
If configs.Count = 0 Then
MsgBox("No settings configured for connection", MsgBoxStyle.Exclamation, "Error")
Dim dResult As Integer = Settings.ShowDialog
If dResult = DialogResult.OK Then
Dim changedConfigs = ConfigurationManager.AppSettings
con.ConnectionString = "Server=" & changedConfigs("appServer") & ";Database=" &
changedConfigs("appDatabase") & ";User=" & changedConfigs("appUser") & ";Pwd=" & changedConfigs("appPass")
Else
Application.Exit()
End If
Else
con.ConnectionString = "Server=" & configs("appServer") & ";Database=" &
configs("appDatabase") & ";User=" & configs("appUser") & ";Pwd=" & configs("appPass")
End If
Try
con.Open()
Dim sageCmd As New SqlCommand("select * from _btblFAAsset", con)
Dim sageDa As New SqlDataAdapter(sageCmd)
Dim sageDs As New DataSet
sageDa.Fill(sageDs)
con.Close()
If Not sageDs.Tables(0).Columns.Contains("ucFARegistrationNumber") And
Not sageDs.Tables(0).Columns.Contains("ulFAType") And
Not sageDs.Tables(0).Columns.Contains("uiFACapacity") And
Not sageDs.Tables(0).Columns.Contains("ucFAMake") Then
MsgBox("Please add the following user defined fields to Sage before importing, " &
"RegistrationNumber as a String of size 200, Capacity of type Integer, Make of type " &
"String of size 200, Model of type String size 200 and Type of type look up with " &
"options '4x4 and Mini Van'", MsgBoxStyle.Exclamation,
"Missing Fields")
Exit Sub
End If
openConnection(con)
Dim cmd As New SqlCommand("select * from [dbo].[_btblFAAsset]", con)
Dim da As New SqlDataAdapter(sageCmd)
Dim ds As New DataSet
da.Fill(ds)
Dim available As New Collection
For local As Integer = 0 To ds.Tables(0).Rows.Count - 1
available.Add(ds.Tables(0).Rows(local).Item("registration_number"))
Next
Dim imported As Integer = 0
Dim values As New List(Of String)
For sage As Integer = 0 To sageDs.Tables(0).Rows.Count - 1
If available.Contains(sageDs.Tables(0).Rows(sage).Item("ucFARegistrationNumber")) Then
Continue For
End If
values.Add("('" & sageDs.Tables(0).Rows(sage).Item("ucFARegistrationNumber") & "', '" &
sageDs.Tables(0).Rows(sage).Item("ulFAType") & "', '" &
sageDs.Tables(0).Rows(sage).Item("ucFAMake") & "', " &
sageDs.Tables(0).Rows(sage).Item("uiFACapacity") & ")")
imported = imported + 1
Next
If values.Count > 0 Then
cmd.CommandText = "INSERT INTO vehicles(registration_number, type, model, capacity) VALUES " &
String.Join(", ", values)
cmd.ExecuteNonQuery()
MsgBox(imported & " assets successfully imported", MsgBoxStyle.Information, "Success")
Else
MsgBox("No new vehicles fetched, local data is upto date", MsgBoxStyle.Information, "Success")
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
答案 0 :(得分:1)
要注意的一些事项:
确保您实际连接的数据库具有该表。
如果表只存在于一个Schema中,则Schema在第一个查询中限定表名,就像您在第二个查询中所做的那样(即dbo._btblFAAsset
)。
或者,如果此表在多个Schema中以相同的名称存在,那么您需要检查要连接的SQL Server登录,包括Login的默认Schema是什么(因为那是它会在哪里寻找物体。)
确保您正在执行正确的SqlCommand。我不确定第二个命令的目的是什么,但它至少看起来看起来是奇数来创建一个新的SqlCommand对象,然后执行第一个:
Dim cmd As New SqlCommand("select * from [dbo].[_btblFAAsset]", con)
Dim da As New SqlDataAdapter(sageCmd)
如您所见,您创建cmd
但将原始sageCmd
传递到SqlDataAdapter
。