Skip to content Skip to sidebar Skip to footer

Sqlserverce Problem With Parameterized Queries From .net

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 what I have found, using

Solution 1:

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()

Post a Comment for "Sqlserverce Problem With Parameterized Queries From .net"