简体   繁体   English

向存储过程调用中添加参数时出错

[英]Error when adding parameters to stored procedure call

In the ShippedContainerSettlement program I am trying to add parameters to a SQL statement on a stored procedure that I created on the remote server (plex). ShippedContainerSettlement程序中,我试图将参数添加到在远程服务器(plex)上创建的存储过程的SQL语句中。

public void checkGradedSerials()
{
    localTable = "Graded_Serials";

    List<string> gradedHides = new List<string>();

    string queryString = "call sproc164407_2053096_650214('@startDate', '" + endDate + "');";
    OdbcDataAdapter adapter = new OdbcDataAdapter();

    OdbcCommand command = new OdbcCommand(queryString, connection);
    command.CommandType = CommandType.StoredProcedure;

    command.Parameters.AddWithValue("@startDate", startDate);
    adapter.SelectCommand = command;

    connection.Open();

    while (rowsCollected == false)
    {
       if (retries <= 5)
       {
          try
          {
              DataTable table = new DataTable();
              adapter.Fill(table);

An error is thrown when I use the parameter @startDate and give it a value. 当我使用参数@startDate并为其提供值时,将引发错误。 However, when I run the program, and add the parameters how I have done for endDate, it runs fine? 但是,当我运行程序并添加参数时,我对endDate的处理方式是否正常?

The error I get back is: 我得到的错误是:

在此处输入图片说明

Any ideas what I am doing wrong. 任何想法我在做什么错。

EDIT: I have incorporated some of the changes mentioned below. 编辑:我已经合并了下面提到的一些更改。 Here is the code I used. 这是我使用的代码。

public void checkGradedSerials()
        {
            localTable = "Graded_Serials";
            List<string> gradedHides = new List<string>();
            OdbcCommand command = new OdbcCommand("sproc164407_2053096_650214", odbcConnection);
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.AddWithValue("@startDate", startDate);
            command.Parameters.AddWithValue("@endDate", endDate);

            OdbcDataAdapter adapter = new OdbcDataAdapter();
            adapter.SelectCommand = command;
            odbcConnection.Open();

            while (rowsCollected == false)
            {
                if (retries <= 5)
                {
                    try
                    {
                        DataTable table = new DataTable();
                        adapter.Fill(table);

But it doesn't seem to be receiving the parameters i am sending through as I am getting this error. 但是由于出现此错误,它似乎没有收到我要发送的参数。

在此处输入图片说明

Here is the stored procedure I am using. 这是我正在使用的存储过程。 This might look odd but remember this is working when I simply pass a string into a select command (see endDate in first code example above). 这可能看起来很奇怪,但是请记住,当我简单地将一个字符串传递给选择命令时,这是可行的(请参见上面第一个代码示例中的endDate)。

SELECT  DISTINCT(Serial_No)
FROM    Part_v_Container_Change2 AS CC
WHERE   CC.Change_Date > @Change_Start_Date AND 
        CC.Change_Date <= @Change_End_Date AND
        CC.Location = 'H Grading';

and the parameters are added here: 并在此处添加参数:

在此处输入图片说明

You should use the System.Data.SqlClient . 您应该使用System.Data.SqlClient You can explicitly declare the datatypes of paramaters you are sending... like this: 您可以显式声明要发送的参数的数据类型,如下所示:

SqlConnection cn;

cn = new SqlConnection(ConnectionString);
SqlCommand cmd;
cmd = new SqlCommand("sproc164407_2053096_650214", cn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@startDate", SqlDbType.DateTime);
cmd.Parameters["@startDate"].Value = startDate;

cmd.Parameters.Add("@enddate", SqlDbType.VarChar);
cmd.Parameters["@enddate"].Value = enddate;

If you must use the ODBC, then you do have to use the ODBC CALL syntax, which does not support named parameters. 如果必须使用ODBC,则必须使用ODBC CALL语法,该语法不支持命名参数。 So change your queryString line to: 因此,将您的queryString行更改为:

string queryString = "{call sproc164407_2053096_650214 (?, ?)}";

Then you can add your parameters: 然后,您可以添加参数:

command.Parameters.Add("@startDate", OdbcType.DateTime).Value=startDate;
command.Parameters.Add("@endDate", OdbcType.DateTime).Value=endDate;

Use SqlCommand instead of odbc. 使用SqlCommand而不是odbc。

Just put the stored proc name in the CommandText, not a SQL statement to execute it. 只需将存储的proc名称放在CommandText中,而不是执行它的SQL语句即可。 Adding the param values means the adapter will pass in the params in the right format. 添加参数值意味着适配器将以正确的格式传递参数。 You don't need to do the string manipulation in CommandText. 您无需在CommandText中进行字符串操作。

If you need to use OdbcCommand then see this answer showing you need to use ? 如果您需要使用OdbcCommand,那么请查看此答案以表明您需要使用? syntax for the parameters, so maybe change your CommandText back to including the 'call' or 'exec' command and parameter placeholders, then make sure you AddWithValue the params in the right order 参数的语法,因此可以将CommandText改回为包含“ call”或“ exec”命令和参数占位符,然后确保以正确的顺序AddWithValueAddWithValue参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM