Where子句不拾取值,为什么

本文关键字:为什么 子句 Where | 更新日期: 2023-09-27 17:49:42

我正在根据registrationNo过滤记录,它应该只选择'1002'的记录,但它没有,它选择所有记录,为什么?

string connStr = ConfigurationManager.ConnectionStrings["ETT"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection sqlCon = new SqlConnection(connStr);
        SqlCommand sqlCom = new SqlCommand("Vehicleledger", sqlCon);
        sqlCom.Parameters.Add("@RegistrationNo", SqlDbType.VarChar);
        sqlCom.Parameters["@RegistrationNo"].Value = "1002";
        SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom);
        DataSet ds = new DataSet();
        sqlDA.Fill(ds);
        try
        {
            sqlCon.Open();
            gvLiveTransaction.DataSource = ds;
            gvLiveTransaction.DataBind();
            sqlCom.ExecuteNonQuery();
        }
        catch (Exception exc)
        {
            Response.Write("Error:" + exc.Message);
        }
        finally
        {
            sqlCon.Close();
        }
    }
}
源代码:

<form id="form1" runat="server">
        <div>
         <asp:GridView ID="gvLiveTransaction" runat="server" DataKeyNames="RNo" 
             AutoGeneratedColumns="false" CellPadding="4" ForeColor="#333333" GridLines="None">
             <AlternatingRowStyle BackColor="White" />
             <EditRowStyle BackColor="#2461BF" />
             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
             <RowStyle BackColor="#EFF3FB" />
             <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
             <SortedAscendingCellStyle BackColor="#F5F7FB" />
             <SortedAscendingHeaderStyle BackColor="#6D95E1" />
             <SortedDescendingCellStyle BackColor="#E9EBEF" />
             <SortedDescendingHeaderStyle BackColor="#4870BE" />
         </asp:GridView>
        </div>
        </form>

我正在调用这个存储过程,当我传递相同的值而不是在asp.net中,它在SQL服务器中工作正常,为什么?

ALTER procedure [dbo].[Vehicleledger]
@RegistrationNo varchar(20)
as
begin
SELECT  dbo.Vehicle.RNo AS RegistrationNo, dbo.TransactionTable.DateOfTransaction, dbo.TransactionTable.RNo, dbo.EngineCapacity.EngineCapcity, 
               dbo.MainCategories.Name, dbo.Categories.Description, dbo.TransactionTable.NoOfMonth, dbo.TransactionTable.Amount
FROM  dbo.Categories INNER JOIN
               dbo.MainCategories ON dbo.Categories.MainCategory_ID = dbo.MainCategories.CatID INNER JOIN
               dbo.TransactionTable ON dbo.Categories.ID = dbo.TransactionTable.CategoryID INNER JOIN
               dbo.EngineCapacity ON dbo.TransactionTable.EngineCapacityID = dbo.EngineCapacity.ID INNER JOIN
               dbo.Vehicle ON dbo.TransactionTable.RNo = dbo.Vehicle.RNo
where dbo.Vehicle.RNo=@RegistrationNo
end

Where子句不拾取值,为什么

我通常这样添加参数。你能在你的代码中试试这个吗?

...
SqlParameter parameter = new SqlParameter("@RegistrationNo", SqlDbType.VarChar);
parameter.IsNullable = false;    
parameter.Size = 20;
parameter.Value = "1002";
sqlCom.Parameters.Add(parameter);
...