使用存储过程从数据库中检索数据

时间:2017-12-01 15:18:22

标签: sql-server asp.net-mvc stored-procedures

我正在使用存储过程从数据库中提取项目列表但是我一直收到此错误:

  

过程或函数'requisition_sp_getItemNum'需要参数'@reqNumber',这是未提供的。

我使用hiddenfor来提供reqNumber但是会抛出相同的错误消息

存储过程:

ALTER PROCEDURE [dbo].[requisition_sp_getItemNum]
    @reqNumber VARCHAR(50)
AS
BEGIN
    SELECT 
        a.ITEMNMBR, a.ITEMDESC, ab.employee_id, 
        ab.department, ab.employee_name, quantity_requested,
        b.expense_acc, c.ACTDESCR + '/' + c.ACTNUMBR_1 + '-' + c.ACTNUMBR_2 [Expense_Acc],
        b.unit_of_measure
    FROM
        [TWCL].[dbo].IV00101 a 
    INNER JOIN
        RequisitionItem b ON a.ITEMNMBR = b.item_no
    INNER JOIN
        Requisition ab ON ab.Req_No = b.Req_No
    INNER JOIN
        [TWCL].dbo.GL00100 c ON b.expense_acc = c.ACTINDX 
    WHERE
        b.Req_No = @reqNumber AND ab.status = -1
END

查看

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <hr/>


          <table id="data">

            <thead>

                <tr>
                    <th></th>
                    <th >@Html.CheckBox("TheOneCheckBoxToRuleThemAll")Select All</th>
                    <th class="col-lg-1 ">Date</th>
                    <th class="col-lg-1 ">Requisition Number</th>
                    <th class="col-lg-1 ">Expense Account</th>
                    <th class="col-lg-1">Requestor</th>
                    <th class="col-lg-1">Department</th>
                    <th class="col-lg-1">LoggedinAs</th>
                    <th class="col-lg-1 ">Item Number</th>
                    <th class="col-lg-1 ">Description</th>
                    <th class="col-sm-1">Quantity</th>
                    <th class="col-sm-1 ">UOM</th>

                    </tr>

            <tbody>





               @for (int i = 0; i < Model.Count; i++)
                {


                   foreach (var item in Model[i].items)
                   {
                    @Html.HiddenFor(m => m[i].reqNumber)
                        <tr>
                            <td>@Html.HiddenFor(m => m[i].reqNumber)</td>
                            <td>@Html.CheckBoxFor(m => m[i].postTrnx, new { @class = "checkGroup1" })</td>
                            <td class="col-lg-1 tabledata" >@Html.DisplayFor(m => m[i].reqDate)</td>
                            <td class="col-lg-1 tabledata" >@Html.DisplayFor(m => m[i].reqNumber)</td>
                            <td class="col-lg-1 tabledata">@item.expense_account.account_desc</td>
                            <td class="col-lg-1 tabledata">@item.employeeDetails.employeeNum</td>
                            <td class="col-lg-1 tabledata">@item.employeeDetails.department</td>
                            <td class="col-lg-1 tabledata">@item.employeeDetails.LoggedInUserName</td>
                            <td class="col-lg-1 tabledata">@item.itemNumber</td>
                            <td class="col-lg-1 tabledata">@item.description</td>
                            <td class="col-sm-1 tabledata">@item.quantity</td>
                            <td class="col-sm-1 tabledata">@item.selecteduomtext </td>

                            @*<td>@Html.ActionLink("Edit", "Edit", new { id = @item.lineNum, name = Model[i].reqNumber })</td>*@
                        </tr>

                    }

                }
            </tbody>
        </table>

模型

 public List<Item> getRequestItemByRquisition(string reqNumber)
    {
        List<Item> items = new List<Item>();
        SqlConnection TWCLOPConnect = new SqlConnection(connectionString.ToString());
        SqlCommand itemscommand = new SqlCommand();
        SqlDataReader itemRdr;
        itemscommand.CommandText = "requisition_sp_getItemNum ";
        itemscommand.CommandType = CommandType.StoredProcedure;
        itemscommand.Connection = TWCLOPConnect;
        itemscommand.Parameters.Add("@reqNumber", SqlDbType.VarChar).Value = reqNumber;

        try
        {
            TWCLOPConnect.Open();
            itemRdr = itemscommand.ExecuteReader();

            while (itemRdr.Read())
            {


                Item item = new Item();

                item.itemNumber = itemRdr.GetString(0);
                item.description = itemRdr.GetString(1);

                item.employeeDetails.employeeNum = Convert.ToInt32(itemRdr.GetString(2));
                item.employeeDetails.department = itemRdr.GetString(3);
                item.employeeDetails.LoggedInUserName = itemRdr.GetString(4);
                item.quantity = Convert.ToDouble(itemRdr[5]);
                item.expense_account.index = itemRdr.GetInt32(6);
                item.expense_account.account_desc = itemRdr.GetString(7);

               item.selecteduomtext = itemRdr.GetString(8);

                items.Add(item);
            }
            itemRdr.Close();
        }
        catch (Exception ex)
        {

            throw ex;
        }
        finally
        {
            TWCLOPConnect.Close();
        }

        return items;
    }

1 个答案:

答案 0 :(得分:0)

您从存储过程@reqNumber变量:

@reqNumber varchar (50) 

以及您在此处添加的变量@Req_No

itemscommand.Parameters.Add("@Req_No", SqlDbType.VarChar).Value = Req_No;

必须匹配。

改变这个:

itemscommand.Parameters.Add("@reqNumber", SqlDbType.VarChar).Value = reqNumber;

到此:

itemscommand.Parameters.Add("@reqNumber", reqNumber);

新方法:您可以将代码更改为此

SqlConnection TWCLOPConnect = new SqlConnection(connectionString.ToString());

SqlCommand itemscommand = new SqlCommand("requisition_sp_getItemNum", TWCLOPConnect);
itemscommand.CommandType = CommandType.StoredProcedure;

itemscommand.Parameters.Add("@reqNumber", reqNumber);

SqlDataReader itemRdr;

try
{

}