从vb.net运行的列名无效

时间:2018-07-23 07:09:02

标签: vb.net sql-server-2008 visual-studio-2008

当我尝试运行vb.net代码时,遇到了“无效列名”错误。此vb.net会将一些参数传递给存储过程。但是,当我尝试从SQL Server Management Studio执行存储过程时,它就可以工作。

下面是我的代码:

    LoadData("tbl_Sources", "sPlayerID,'',sPlayerName", "tbl_Dest", "dPlayerID,"",dPlayerName")

------------------------------------------

Public Function LoadData(ByVal frmTblname As String, ByVal frmClmnName As String, ByVal toTblName As String, ByVal toClmnName As String) As Integer

    Dim con As New SqlClient.SqlConnection
    Dim conManager As New Connection
    Dim sqlParams() As SqlParameter = New SqlParameter() { _
        New SqlParameter("@fromTable", SqlDbType.NVarChar, 50) With {.Value = frmTblname}, _
        New SqlParameter("@fromColumn", SqlDbType.Structured, 4000) With {.Value = "'" + frmClmnName + "'"}, _
        New SqlParameter("@toTable", SqlDbType.NVarChar, 50) With {.Value = toTblName}, _
        New SqlParameter("@toColumn", SqlDbType.NVarChar, 4000) With {.Value = "'" + toClmnName + "'"} _
        }
    Dim err As Integer

    con = conManager.GetConnection(sConn)      
    err = ConnectionExec.RunSPReturnInteger(con, "insertUnknownDir", sqlParams)
    Return err
End Function

---------------------------------------------------------
Public Function RunSPReturnInteger(ByVal con As SqlClient.SqlConnection, ByVal strSP As String, ByVal ParamArray commandParameters() As SqlClient.SqlParameter) As Integer
    Dim retVal As Integer

    LogManager.WriteLog("#################################### Start " + strSP + " ####################################")
    Try
        Dim cmd As New SqlCommand(strSP, con)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandTimeout = 0
        Dim p As SqlClient.SqlParameter

        For Each p In commandParameters
            p = cmd.Parameters.Add(p)
            p.Direction = ParameterDirection.Input
            p.SqlDbType = SqlDbType.Structured              
        Next

        retVal = cmd.ExecuteNonQuery()

        cmd.Dispose()
        Return retVal
        LogManager.WriteLog("Try RunSPReturnInteger 1 -> SP Name : " + strSP + " > " + retVal.ToString)
    Catch ex As Exception
        'MsgBox(ex.Message)
        LogManager.WriteLog("Catch RunSPReturnInteger 1-> SP Name : " + strSP + ">> " + ex.Message)
        Throw New Exception(ex.Message, ex)
        Return -1
    End Try        
End Function

这是我的存储过程:

ALTER procedure [dbo].[insertUnknownDir] @fromTable nvarchar(50),@fromColumn nvarchar(4000),@toTable nvarchar(50),@toColumn nvarchar(4000) 
as 
begin 
declare @Query nvarchar(4000)   print 'exec [insertUnknownDir] '+@fromTable+' ,'+@fromColumn+' ,'+@toTable+','+@toColumn+'' 
SET @Query ='INSERT INTO ['+@toTable+']('+@toColumn+') 
SELECT TOP 15 '+@fromColumn+' 
FROM '+@fromTable+'
WHERE profile.sPlayerID = '+@fromTable+'.sPlayerID'     
 exec sp_executesql @Query
END

感谢任何建议/帮助!

1 个答案:

答案 0 :(得分:0)

由于您使用动态SQL,因此可能会组合失败的参数。 最好运行SQL事件探查器以捕获在特定数据库上执行的SQL语句,以查看引起错误的确切SQL查询