如何使用参数输入在按钮单击上执行存储过程

时间:2014-07-19 03:23:21

标签: c# asp.net stored-procedures

我是asp.net的新手。我被困在按钮单击以执行带有两个inout参数的存储过程。

  • 输入参数:StartConsoleClosingID, EndConsoleClosingID
  • 存储过程:ConsoleClosingIDSearch
  • 数据:GridView

标记:

<div>
    Start ID<asp:TextBox ID="TextBox1" runat="server" Width="107px"></asp:TextBox>
    End ID<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Seacrh" />
    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
            AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="PaymentConsoleClosingID"
                 HeaderText="PaymentConsoleClosingID" 
                 SortExpression="PaymentConsoleClosingID" />
            <asp:BoundField DataField="Employer SOCSO Sum" 
                 HeaderText="Employer SOCSO Sum" 
                 ReadOnly="True" SortExpression="Employer SOCSO Sum" />
            <asp:BoundField DataField="Employee SOCSO Sum" 
                 HeaderText="Employee SOCSO Sum" 
                ReadOnly="True" SortExpression="Employee SOCSO Sum" />
            <asp:BoundField DataField="Employer EPF Sum" 
                 HeaderText="Employer EPF Sum" 
                 ReadOnly="True" SortExpression="Employer EPF Sum" />
            <asp:BoundField DataField="Employee EPF Sum" 
                 HeaderText="Employee EPF Sum" ReadOnly="True" 
                 SortExpression="Employee EPF Sum" />
            <asp:BoundField DataField="Amount Sum" HeaderText="Amount Sum"
                 ReadOnly="True" SortExpression="Amount Sum" />
            <asp:BoundField DataField="DepartmentID" HeaderText="DepartmentID" 
                SortExpression="DepartmentID" />
            <asp:BoundField DataField="Account Code" HeaderText="Account Code" 
                ReadOnly="True" SortExpression="Account Code" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:CLMSConnectionString %>" 
            SelectCommand="ConsoleClosingIDSearch" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:ControlParameter ControlID="TextBox1" Name="StartConsoleClosingID" 
                PropertyName="Text" Type="String" />
            <asp:ControlParameter ControlID="TextBox2" Name="EndConsoleClosingID" 
                PropertyName="Text" Type="String" />
        </SelectParameters>
        </asp:SqlDataSource>
    </div>

2 个答案:

答案 0 :(得分:0)

您可以在代码后面绑定GridView。你需要编写一些代码来绑定cs文件中的datagridview。

SqlCommand Cmd = Connection.CreateCommand();
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.CommandText = "ConsoleClosingIDSearch";
Cmd.Parameters.Add("@StartConsoleClosingID", SqlDbType.Int).value = Convert.ToInt32(TextBox1.Text);
Cmd.Parameters.Add("@EndConsoleClosingID ", SqlDbType.Int).value = Convert.ToInt32(TextBox2.Text);

SqlDataAdapter Da = New SqlDataAdapter(Cmd);
DataTable dt = New DataTable();
Da.Fill(dt);

GridView1.DataSource = dt;
GridView1.DataBind();

或者您可以参考here

中的完整示例

答案 1 :(得分:0)

这里是使用Sqlhelper的另一个代码示例。

string conString = "your connection string";
SqlConnection con = new SqlConnection(conString);
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("@userid", userid);
param[1] = new SqlParameter("@param2", param2);

DataSet ds = SqlHelper.ExecuteDataset(con, CommandType.StoredProcedure, "usp_GetUserData", param);
GridView1.DataSource = ds;
GridView1.DataBind();