从SQL-Server直接读取单个PK记录不会返回任何内容 [英] Direct read of single PK record from SQL-Server returns nothing
问题描述
我确定我必须遗漏一些非常基本或简单的东西,但我只是看不到它!
我有一个SQL-服务器表名为位置
,包含2列, LocationKey
& LocationPath
。
我在该表中有1条记录, LocationKey
= LocationDespatchSchedules & LocationPath
= D:\
我对该表的访问权限是通过以下代码:
I'm sure I must be missing something pretty basic or simple here, but I just can't see it !!!
I have a SQL-Server Table called Locations
, with 2 Columns, LocationKey
& LocationPath
.
I have 1 record in that Table, the LocationKey
= LocationDespatchSchedules & the LocationPath
= D:\
My access to that table is via the following code :
Public Sub Get_Location(myFile As String, myType As String)
Dim mySqlConn As SqlConnection = New SqlConnection(myDB)
Dim mySqlString As String = "SELECT LocationPath FROM Locations WHERE LocationKey = 'Location" & myFile.Replace(" ", "") & "'"
Dim mySqlCmd As SqlCommand
Dim myObject As Object
Try
mySqlConn.Open()
mySqlCmd = New SqlCommand(mySqlString, mySqlConn)
myObject = mySqlCmd.ExecuteScalar()
If myObject IsNot Nothing Then
myFileLocation = myObject.ToString
Else
MessageBox.Show("Error - No Record Found")
End If
Catch ex As Exception
myAbortCode = "2"
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
MessageBox.Show("Program Aborted") 'Store error details in file for copying
Exit Sub
Finally
mySqlConn.Close()
End Try
End Sub
价值在此次运行中传入 myFile
是... 发货时间表
价值执行行中的mySqlCommand是......
The value passed into myFile
in this run is ... Despatch Schedules
The value of mySqlCommand in the execution line is ...
SELECT LocationPath FROM Locations WHERE LocationKey = 'LocationDespatchSchedules'
据我所知,这是与Key的匹配,因此它应该落入 myFileLocation
= myObject.ToString 行,但它没有,它返回Nothing并且总是落入错误消息。
我做错了什么?!?
As far as I can see, that is a match to the Key and it should therefore drop into the myFileLocation
= myObject.ToString line, but it doesn't, it returns Nothing and always falls through to the Error message.
What have I done wrong ?!?
推荐答案
问题是由于地点表未被复制到DataSet!
The problem was caused because the Locations Table hadn't been copied into the DataSet !
这篇关于从SQL-Server直接读取单个PK记录不会返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!