0

我对 sql server 非常陌生,我正在创建一个 web 服务,并且使用下面的代码,我得到了上面的错误,我想知道我做错了什么?

<WebMethod()> _
Public Function GetAddresses(ByVal skip As Integer, ByVal take As Integer) As FuelStop()
    Dim resultList = New List(Of FuelStop)()
    Using sqlCon As New SqlConnection()
        sqlCon.ConnectionString = "Data Source=(local);Initial Catalog=DEV_DB;User ID=*****;Password=**********"
        Dim command As New SqlCommand("SELECT * FROM Gas_Stations WHERE Location_Type = 1 AND [ Physical_Address_Street] = @Physical_Address_Street AND [ Physical_Address_Local] = @Physical_Address_Local AND [Physical_Address_State] = @Physical_Address_State AND [ Physical_Address_Zip] = @Physical_Address_Zip AND [ Phone_Number] = @Phone_Number")
        command.Parameters.Add("@Physical_Address_Street", SqlDbType.VarChar, 50, "Physical_Address_Street")
        command.Parameters.Add("@Physical_Address_Local", SqlDbType.VarChar, 50, "Physical_Address_Local")
        command.Parameters.Add("@Physical_Address_State", SqlDbType.VarChar, 50, "Physical_Address_State")
        command.Parameters.Add("@Physical_Address_Zip", SqlDbType.VarChar, 50, "Physical_Address_Zip")
        command.Parameters.Add("@Phone_Number", SqlDbType.VarChar, 50, "Phone_Number")

        command.Connection = sqlCon
        sqlCon.Open()

        Using reader = command.ExecuteReader()
            While reader.Read()
                Dim addr = New FuelStop()
                addr.Physical_Address_Street = reader.GetString(0)
                addr.Physical_Address_Local = reader.GetString(1)
                addr.Physical_Address_State = reader.GetString(2)
                addr.Physical_Address_Zip = reader.GetString(3)
                addr.Phone_Number = reader.GetString(4)

                resultList.Add(addr)
            End While
        End Using
    End Using
    Return resultList.Skip(skip).Take(take).ToArray()
End Function

我希望直接从数据库中提取查询中列出的列的值。我需要在 android 应用程序中显示所有地址信息。这将是只读情况。

4

2 回答 2

2

数据库中的列名可能不以空格开头,所以...

Dim command As New SqlCommand("SELECT * FROM Gas_Stations WHERE Location_Type = 1 AND [Physical_Address_Street] = @Physical_Address_Street AND [Physical_Address_Local] = @Physical_Address_Local AND [Physical_Address_State] = @Physical_Address_State AND [Physical_Address_Zip] = @Physical_Address_Zip AND [Phone_Number] = @Phone_Number")

但是您可以利用 VB.NET XML 文字使其更易于阅读:

Dim sql = <sql>
        SELECT
            [Physical_Address_Street]
            , [Physical_Address_Local]
            , [Physical_Address_State]
            , [Physical_Address_Zip]
            , [Phone_Number]
        FROM Gas_Stations
        WHERE Location_Type = 1
            AND [Physical_Address_Street] = @Physical_Address_Street
            AND [Physical_Address_Local] = @Physical_Address_Local
            AND [Physical_Address_State] = @Physical_Address_State
            AND [Physical_Address_Zip] = @Physical_Address_Zip
            AND [Phone_Number] = @Phone_Number
        </sql>

Dim command As New SqlCommand()
command.CommandText = CStr(sql)

请注意,我明确命名了要选择的列。这是为了确保您想要返回的列是您获得的列并且按照您想要的顺序。

至于预期的参数错误消息,您尚未为任何参数分配值。你可以这样做:

command.Parameters.Add("@Physical_Address_Street", SqlDbType.VarChar, 50).Value = physAddrStreet

或像这样:

command.Parameters.Add("@Physical_Address_Street", SqlDbType.VarChar, 50)
command.Parameters("@Physical_Address_Street").Value = physAddrStreet

编辑:因为你不需要所有这些参数,

Dim sql = <sql>
        SELECT
            [Physical_Address_Street]
            , [Physical_Address_Local]
            , [Physical_Address_State]
            , [Physical_Address_Zip]
            , [Phone_Number]
        FROM Gas_Stations
        WHERE Location_Type = 1
        </sql>

并且不要做所有的command.Parameters.Add(...).

于 2013-04-18T17:54:37.350 回答
1

查看上面的查询,您似乎想要显示固定位置类型的一组动态属性(有时是街道,有时是邮政编码等)。

SQL Server 中的参数化查询旨在使用输入参数,而不是使用您希望在结果中看到的列。

如果您真的希望在结果中设置动态列,请查看有关如何执行此操作的问题。

相反,如果您正在寻找用户输入查询的地址、邮编等,那么您的查询会稍微倒退。它应该如下所示:

Dim command As New SqlCommand
   ("SELECT * FROM Gas_Stations 
      WHERE Location_Type = 1
            AND
            Physical_Address_Street = @Physical_Address_Street 
            AND 
            Physical_Address_Local = @Physical_Address_Local 
            AND 
            Physical_Address_State = @Physical_Address_State
            AND
            Physical_Address_Zip = @Physical_Address_Zip
            AND 
            Phone_Number = @Phone_Number
    )

注意:您需要为 VB.Net 清理上面的格式,我这样做是为了提高可读性。如果您想让这些字段可选地可搜索,还有其他问题

于 2013-04-18T16:42:21.197 回答