错误 - 必须声明标量变量 - C#

时间:2016-07-19 08:24:37

标签: c# mysql asp.net variables scalar

我正在使用C#将文件上传到文件夹中。在我点击上传后,它出现错误并且错误“必须声明标量变量”@TABLE_ID“。我做了一些研究并尝试使用其他例子,但错误仍然存​​在。这是上传功能:

protected void UploadFile(object sender, EventArgs e)
    {
        string str = ConfigurationManager.ConnectionStrings["Ulysses"].ConnectionString;
        string filenam = FileUpload1.FileName.ToString();
        string TABLE_NAME = "REQUEST";

        int table_id = -1;
        table_id = generateNextId("SP_LINK_TABLE_FIELD_ID");

        string path = @"C:\Users\muhd\Documents\CyberLink";

        path = path + filenam;
        FileUpload1.PostedFile.SaveAs(path);

       SqlConnection con = new SqlConnection(str);
       SqlCommand cmd = new SqlCommand();
       cmd.CommandText = "Insert into OLE(LINK_TABLE_ID, LINK_TABLE_NAME, OLE_LINK_FILE_NAME) values(@TABLE_ID,@TABLE_NAME,@FILE_NAME)";
       cmd.CommandType =  CommandType.Text;
       command.Parameters.AddWithValue("@TABLE_ID", SqlDbType.Int).Value = table_id;
       command.Parameters.AddWithValue("@TABLE_NAME", TABLE_NAME);
       command.Parameters.AddWithValue("@FILE_NAME",path);
       cmd.Connection = con;
       con.Open();
       cmd.ExecuteNonQuery();
       con.Close();
    }

table_id是使用其他函数从存储过程中获取的。请参考以下内容:

protected int generateNextId(string strStoredProcedureName)//this is for inserting table_field_id for uploading attachment
    {
        int intNewId = 0;
        try
        {
            strConn = ConfigurationManager.ConnectionStrings["Ulysses"].ToString();
            conn = new SqlConnection(strConn);
            command = new SqlCommand(strStoredProcedureName, conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@newid", SqlDbType.Int);
            command.Parameters["@newid"].Direction = ParameterDirection.ReturnValue;
            conn.Open();
            command.ExecuteNonQuery();
            intNewId = (int)command.Parameters["@newid"].Value;
        }
        catch (Exception ex)
        {

        }
        finally
        {
            conn.Close();
            conn = null;
        }
        return intNewId;
    }
   }

这是我使用的存储过程:

    SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_LINK_TABLE_FIELD_ID]
AS
begin tran;
declare @nextid integer;
update generator set current_number = current_number + 1 where generator = 'LINK_TABLE_FIELD';
select @nextid = current_number from generator where generator = 'LINK_TABLE_FIELD';
commit tran;
return @nextid;

我不确定为什么它需要声明甚至其他具有相同样式的函数对代码没有问题,但是这个显示了这个错误。我希望任何人都能对这个问题有所了解。谢谢你提前。

1 个答案:

答案 0 :(得分:3)

您正在执行的命令是cmd而不是command。您没有将参数添加到正确的命令对象。

command.Parameters.AddWithValue("@TABLE_ID", SqlDbType.Int).Value = table_id;
command.Parameters.AddWithValue("@TABLE_NAME", TABLE_NAME);
command.Parameters.AddWithValue("@FILE_NAME",path);

应该是:

cmd.Parameters.AddWithValue("@TABLE_ID", SqlDbType.Int).Value = table_id;
cmd.Parameters.AddWithValue("@TABLE_NAME", TABLE_NAME);
cmd.Parameters.AddWithValue("@FILE_NAME",path);