SQLServerCE Problem with parameterized queries from .NET

前端 未结 1 559
南方客
南方客 2020-12-21 14:29

I am pulling the hair out of my head trying to figure this one out.

I can\'t make Parameterized queries to work in VB.Net, when I am using parameters.

From w

相关标签:
1条回答
  • 2020-12-21 15:26

    Finally, I have found the solution for this problem.

    Using a parameter in a function crashes if the DBType property of the parameter is not set:

    This will crash:

        Dim cmd As SqlCeCommand = db.CreateCommand()
    
        cmd.CommandText = "SELECT COALESCE(@param1, @param2);"
        cmd.Parameters.Add("@param1", 1)
        cmd.Parameters.Add("@param2", "test")
        cmd.ExecuteScalar()
    

    Using a parameter in a function will work if the DBType property of the parameter is set

    This will work:

        Dim cmd As SqlCeCommand = db.CreateCommand()
    
        cmd.CommandText = "SELECT COALESCE(@param1, @param2);"
        cmd.Parameters.Add("@param1", 1).DbType = DbType.Int32
        cmd.Parameters.Add("@param2", "test").DbType = DbType.String
        cmd.ExecuteScalar()
    
    0 讨论(0)
提交回复
热议问题