SQL Anywhere查询错误:主机变量的值不足 [英] SQL anywhere query error: Not enough values for host variables

查看:327
本文介绍了SQL Anywhere查询错误:主机变量的值不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.Net中的ODBC命令对象构建查询,并传入了多个参数.对SQL Anywhere执行查询时,出现以下错误. (相同的代码适用于SQL Server).

I am building a query using ODBC command object in .Net with multiple parameters being passed in. When executing the query against SQL Anywhere, I get the following error. (The same code works against SQL Server).

[System.Data.Odbc.OdbcException] = {错误[07002] [Sybase] [ODBC驱动程序] [SQL Anywhere]主机变量的值不足"}

[System.Data.Odbc.OdbcException] = {"ERROR [07002] [Sybase][ODBC Driver][SQL Anywhere]Not enough values for host variables"}

命令对象添加的参数数量与查询中的占位符('?')相同.以下是一个简单的查询和未通过测试的C#代码.

The command object has the same number of parameters added as the place holders ('?') in the query. Following is a simple query and C# code that fails the test.

用于填充主机变量的C#代码

C# code to populate the host variables

String queryText = @"DECLARE @loanuseraddress varchar(40),@ loanid十进制 设置@loanid =? 设置@loanuseraddress =? 从loan_assignments中选择*,其中loan_id = @loanid"

String queryText= @"DECLARE @loanuseraddress varchar(40), @loanid decimal Set @loanid = ? Set @loanuseraddress = ? select * from loan_assignments where loan_id = @loanid"

        OdbcConnection connection = new OdbcConnection(request.ConnectionString);
        OdbcCommand command;    

        command = new OdbcCommand(queryText, connection);

        OdbcParameter param1 = new OdbcParameter("@loanid", OdbcType.Decimal);
        param1.Value = request.Loan.LoanNumber;
        command.Parameters.Add(param1);

        OdbcParameter param2 = new OdbcParameter("@loanuseremployer", dbcType.VarChar);
        param2.Value = appraisalCompanyUpdate.LoanUserEmployer;
        if (param2.Value == null)
            param2.Value = DBNull.Value;
        command.Parameters.Add(param2);


        connection.Open();  
        OdbcDataReader rows = command.ExecuteReader();

推荐答案

我通过检查null来解决此问题.当您尝试将NULL参数传递给Sybase时,这就是您得到的错误(至少对我而言).感觉LoanId在某些时候为空.

I fixed this by checking for nulls. When you try to pass a null parameter to Sybase, that's the error you get (at least for me). Have a feeling LoanId is null at some point.

编辑经过更多研究,我认为当您尝试通过.Net中的Sybase ODBC Connection进行多次插入/删除/更新时,也会出现此错误.我认为这不受支持,MSDN似乎说这是特定于供应商的.

Edit After doing a little more research, I think you can also get this error when you try multiple insert / deletes / updates through the Sybase ODBC Connection in .Net. I don't think this is supported and MSDN seems to say it's vendor specific.

这篇关于SQL Anywhere查询错误:主机变量的值不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆