Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
808 views
in Technique[技术] by (71.8m points)

sql server ce - 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 a parameter in a function, from .NET raises an error (see sample code). However, running the not working query in the Query Window in Visual studio works properly.

The error raised is:

25922 - The arguments for function are not valid.

Info from: http://msdn.microsoft.com/en-us/library/aa256772%28SQL.80%29.aspx

Sample Code:

Imports System.Data.SqlServerCe

Public Class MiniDemo

    Public Shared Sub Main()
        Dim cs As String = "Data Source=Inventory.sdf; Persist Security Info=False;"
        Dim db As New SqlCeConnection(cs)
        db.Open()


        ''#Using parameters works
        Dim cmd As SqlCeCommand = db.CreateCommand()

        cmd.CommandText = "INSERT INTO inventory_type (inv_type_id, inv_type_name) VALUES (@id, @name)"
        cmd.Parameters.Add("@id", 1)
        cmd.Parameters.Add("@name", "test")
        cmd.ExecuteNonQuery()



        ''#Using parameters in functions does not work
        Dim cmd2 As SqlCeCommand = db.CreateCommand()

            ''#Broken on multiple lines for readability (built using string builders in real code)
        cmd2.CommandText = 
                     "SELECT 
                        inv_type_id, 
                        inv_type_name 
                      FROM
                        inventory_type 
                      WHERE 
                        ((@id IS NULL) OR (inv_type_id = @id)) AND 
                        ((@name IS NULL) OR (inv_type_name = @name))"

        cmd2.Parameters.Add("@id", 1)
        cmd2.Parameters.Add("@name", "test") ''# Would not work with DBNull.Value either

        Dim da2 As New SqlCeDataAdapter(cmd2)
        Dim dt2 As New DataTable()
        da2.Fill(dt2)
        db.Close()
    End Sub

End Class
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...