这是Command Object的正确格式吗?

时间:2015-06-23 05:45:11

标签: asp.net vb.net sql-server-2008 ado.net

    Public Function GET_B(ByVal CampusCode As String) As DataSet

        Dim objConn As New SqlConnection(Connection.ConnectionString)

        Dim strSQL As String
        strSQL = "SELECT BldgCode "
        strSQL += "FROM BuildingPublicExt "
        strSQL += "WHERE (CampusCode = N'" & "@CampusCode" & "') "
        strSQL += "AND (BldgStatusCode = N'A') "
        strSQL += "ORDER BY BldgCode"

        Dim cmd As New SqlCommand(strSQL)
        cmd.Parameters.AddWithValue("@CampusCode", CampusCode)

        Dim ad As New SqlDataAdapter(cmd, objConn)
        Dim ds As New DataSet
        ad.Fill(ds, "Result")

        Return ds
        objConn.Close()
    End Function

是" Dim cmd As New SqlCommand(strSQL)"的格式是正确的..?或者我需要在参数中添加任何连接对象..

1 个答案:

答案 0 :(得分:0)

您需要将SqlConnection对象作为第二个参数in构造函数of SqlCommand`传递。

Dim cmd As New SqlCommand(strSQL,objConn);

如果您执行此操作,则无需在SqlConnection的{​​{1}}中传递constructor个对象。

SqlDataAdapter

如果您在Dim ad As New SqlDataAdapter(cmd); //Remove objConn from here. 中传递SqlConnection对象,则第一次评估应该是SqlAdapter constructor类型,这将是您的查询。