如何在代码后面使用存储过程调用多个参数?

时间:2014-05-07 07:55:38

标签: c# asp.net sql-server stored-procedures

我被困了两周以获得解决方案。

我正在开发一个小型应用程序,它是一个呼叫日志系统。我的问题是当我想搜索我的日志表单时,不会返回数据。我已经检查了我的存储过程查询。我可以成功运行它,但我不知道如何将我的参数回调到后面的代码中。任何人都可以给我一些例子。

这是我的存储过程:

ALTER PROCEDURE [dbo].[T_FILTER]
  (
   @Call_LogID int= null,  
   @StarDate datetime= null ,
   @EndDate datetime =null,
    @Log_code char(20)= null

   )                
    AS
    Select Call_LogID  ,Log_status ,Log_code  ,Log_TypeApp ,Log_Branch ,Log_TypeCust ,Log_name
 From Call_Log_Service Where EntryTime BETWEEN convert(Datetime,@StarDate,102) 
AND convert(Datetime,@EndDate,102)
AND (Call_logID LIKE '%@Call_logID')
  AND (Log_code Like  '%@Log_code') 
ORDER  BY Call_LogID

这是我的代码背后的代码

<%@ Page Title="" Language="C#" MasterPageFile="~/User/MasterPage.Master" AutoEventWireup="true" CodeBehind="Filter.aspx.cs" Inherits="Call_LogSystem.User.WebForm3" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <style type="text/css">
        .style1
        {
            width: 98px;
            text-align: left;
            height: 26px;
        }

        .style5
        {
            width: 98px;
            text-align: center;
            height: 26px;
        }
        .style6
        {
            width: 79px;
            height: 29px;
        }
        .style7
        {
            width: 95px;
            height: 29px;
        }
        .style10
        {
            width: 122px;
            height: 29px;
        }

        .style11
        {
            width: 121px;
            height: 29px;
        }
        .style12
        {
            width: 83px;
            height: 29px;
        }
        .style13
        {
            width: 78px;
            height: 29px;
        }

        .style16
        {
            width: 132px;
            text-align: center;
            height: 26px;
        }

        </style>
    <script type="text/javascript">
        function Showalert(Log_name) {
            alert(Log_name + ' details updated successfully.');
            if (alert) {
                window.location = 'Default.aspx';
            }
        }
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div style="height: 643px; width: 935px">
<table style="height: 85px; width: 217px; text-align: left; font-weight: 700;" 
            align="center" border="2">
 <asp:ScriptManager ID="script" runat="server">
</asp:ScriptManager>
<tr>

<td class="style16">
    <strong>StartDate:
</strong>
</td>
<td class="style5">  

<asp:TextBox ID="tbStartDate" runat="server" Height="22px" Width="128px"> 
       </asp:TextBox>
    <ajaxToolkit:CalendarExtender ID="ce1" runat="server" Format="dd/MM/yyyy" TargetControlID="tbStartDate" />
    <script type="text/javascript">
        $('#<%= tbStartDate.ClientID %>').datepicker();
</script>
  </td>
</tr>
<tr>
<td class="style16">
    <strong>EndDate:
</strong>
</td>
<td class="style1">
<asp:TextBox ID="tbEndDate" runat="server" Height="22px" Width="128px" 
        style="text-align: left; margin-left: 0px;" >
       </asp:TextBox> 

<ajaxToolkit:CalendarExtender ID="ce2" runat="server" Format="dd/MM/yyyy" TargetControlID="tbEndDate" />
  <script type="text/javascript">
      $('#<%= tbEndDate.ClientID %>').datepicker();
</script>
</td>
</tr>
</table>
<br />
<fieldset>
<legend>Search By<strong> Date</strong></legend>
  <table style="height: 59px; width: 854px" border="0">

  <tr>  

<td class="style6">
    Log No</td>
    <td class="style7">
    <asp:TextBox ID="txtLog" runat="server" Height="25px" Width="38px"  
        AutoPostBack="True" Text='<%# Eval("Call_logID") %>' ></asp:TextBox>
      </td>
<td class="style13"> Client Code</td>

<td class="style12">


    <asp:TextBox ID="txtClient" runat="server" Height="23px" Width="69px" 
        AutoPostBack="True" Text='<%# Eval("Log_code") %>'></asp:TextBox>
      </td>


<td class="style10">


    <%--Category</td>--%>
    </td>

<td class="style11">



      </td>
      <tr>
      <td>
      <asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" 
              Width="61px" />
      </td>

      </tr>

</table>
</fieldset>

 <asp:GridView ID="GridView1" runat="server" 
            AllowPaging="True"  PageSize="5" AutoGenerateColumns="False" 
            Height="353px" Width="796px" style="margin-left: 82px; text-align: left;" 
            EmptyDataText="No records found" 
            onpageindexchanging="GridView1_PageIndexChanging">
                <AlternatingRowStyle BackColor="White" />
            <Columns>
           <asp:TemplateField>
<ItemTemplate>
<a href ='<%#"Call_LogUpdate.aspx?Call_logID="+DataBinder.Eval(Container.DataItem,"Call_logID") %>'><%#Eval("Call_logID")%></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Call_logID" HeaderText="Log ID" />
                <asp:BoundField DataField="Log_TypeCust" HeaderText="Type" />
<asp:BoundField DataField="Log_name" HeaderText="Name" />
                <asp:BoundField DataField="EntryTime" HeaderText="DateIn" />
                <asp:BoundField DataField="Log_code" HeaderText="Client Code" />
<asp:BoundField DataField="Log_Status" HeaderText="Status" />
                <asp:BoundField DataField="Log_TypeApp" HeaderText="Application" />
                <asp:BoundField DataField="Log_Branch" HeaderText="Branch" />
</Columns>

            <EditRowStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#8C4510" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />

</asp:GridView>
    <asp:Label ID="lblError" runat="server" Text=""></asp:Label>
</div>


</asp:Content>

1 个答案:

答案 0 :(得分:1)

这样的事情怎么样:

        using System.Data;
        using System.Data.SqlClient;

        int callLogId = 0;
        DateTime startDate = DateTime.Now;
        DateTime endDate = DateTime.Now;
        string logCode = "";
        string connectionString = "YourConnectionString";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand("T_FILTER", connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("Call_LogID",callLogId);
            cmd.Parameters.AddWithValue("StarDate",startDate);
            cmd.Parameters.AddWithValue("EndDate",endDate);
            cmd.Parameters.AddWithValue("Log_code",logCode);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
              //do something
            }
            dr.Dispose();
            cmd.Dispose();
        }

我注意到存储过程中的一个参数被称为'StarDate' - 不确定这是否是拼写错误并且应该是'StartDate'?