将数据传递给存储过程并返回数据给gridview
本文关键字:数据 返回 gridview 存储过程 | 更新日期: 2023-09-27 18:16:18
我正在编写一个asp .net网页,将文本框数据传递给存储过程,并在按钮单击上返回数据的gridview。我已经开始编写代码,但我对如何允许项目控制页面上的动作感到困惑。例如,让按钮点击将字段值传递给存储过程以获取数据。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>MSS Archived Data</title>
<script language="javascript" type="text/javascript">
// <!CDATA[
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="First Name: "></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Last Name: "></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="Street: "></asp:Label>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="City: "></asp:Label>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:Label ID="Label5" runat="server" Text="State: "></asp:Label>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:Label ID="Label6" runat="server" Text="Zip: "></asp:Label>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" />
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="actID" DataSourceID="PersEWD_Database">
<Columns>
<asp:BoundField DataField="actID" HeaderText="actID" ReadOnly="True" InsertVisible="False"
SortExpression="actID"></asp:BoundField>
<asp:BoundField DataField="actLogCreateDate" HeaderText="actLogCreateDate" SortExpression="actLogCreateDate">
</asp:BoundField>
<asp:BoundField DataField="actCustomerID" HeaderText="actCustomerID" SortExpression="actCustomerID">
</asp:BoundField>
<asp:BoundField DataField="actCampaignID" HeaderText="actCampaignID" SortExpression="actCampaignID">
</asp:BoundField>
<asp:BoundField DataField="actActionDate" HeaderText="actActionDate" SortExpression="actActionDate">
</asp:BoundField>
<asp:BoundField DataField="actActionCode" HeaderText="actActionCode" SortExpression="actActionCode">
</asp:BoundField>
<asp:BoundField DataField="actRelevantInfo" HeaderText="actRelevantInfo" SortExpression="actRelevantInfo">
</asp:BoundField>
<asp:BoundField DataField="actUnderwriter" HeaderText="actUnderwriter" SortExpression="actUnderwriter">
</asp:BoundField>
<asp:BoundField DataField="actAmount" HeaderText="actAmount" SortExpression="actAmount">
</asp:BoundField>
<asp:BoundField DataField="actPolicyType" HeaderText="actPolicyType" SortExpression="actPolicyType">
</asp:BoundField>
<asp:BoundField DataField="actPolicyNumber" HeaderText="actPolicyNumber" SortExpression="actPolicyNumber">
</asp:BoundField>
<asp:BoundField DataField="actReasonCode" HeaderText="actReasonCode" SortExpression="actReasonCode">
</asp:BoundField>
<asp:BoundField DataField="actSSN" HeaderText="actSSN" SortExpression="actSSN"></asp:BoundField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Web.Configuration;
public partial class _Default : System.Web.UI.Page
{
DataSet ds = new DataSet();
//Here we declare the parameter which we have to use in our application
SqlCommand cmd = new SqlCommand();
SqlParameter sp1 = new SqlParameter();
SqlParameter sp2 = new SqlParameter();
SqlParameter sp3 = new SqlParameter();
SqlParameter sp4 = new SqlParameter();
SqlParameter sp5 = new SqlParameter();
SqlParameter sp6 = new SqlParameter();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//string myConnection = "dsn=dsnNAME";
string serverName = "serverName";
string dbName = "dbName";
string storedProcName = "";
string parameterName = "@RecordType";
int parameterValue = 0;
string myConnString = "DRIVER={SQL Server};SERVER="+serverName+";Trusted_connection=yes;DATABASE="+dbName+";";
OdbcConnection myConnection = new OdbcConnection(myConnString);
myConnection.Open();
//myConnection.Connection = myConnection;
myConnection.CommandType = CommandType.StoredProcedure;
myConnection.CommandText = "{call " + storedProcName + " (?, ?, ?, ?, ?, ?) }";
cmd.Parameters.Add("@fName", SqlDbType.VarChar).Value = TextBox1.Text;
cmd.Parameters.Add("@lName", SqlDbType.VarChar).Value = TextBox2.Text;
cmd.Parameters.Add("@street", SqlDbType.VarChar).Value = TextBox3.Text;
cmd.Parameters.Add("@city", SqlDbType.VarChar).Value = TextBox4.Text;
cmd.Parameters.Add("@state", SqlDbType.VarChar).Value = TextBox5.Text;
cmd.Parameters.Add("@zip", SqlDbType.varchar).Value = TextBox6.Text;
myConnection.ExecuteNonQuery();
myConnection.Connection.Close();
}
}
谢谢
数据绑定的生命周期看起来有点像这样:
-
Button Click -当用户按下按钮时,按钮将触发Click事件。从这里开始,您将使用TextBox中的值调用存储过程。
-
数据检索——使用指定的参数执行存储过程,并返回一组数据元素。根据你使用的。net框架的版本,我建议使用TableAdapters(旧技术,给你一个镜像数据库的表结构)或使用LINQ/Entity Framework(新技术,给你一个基于表的对象,并提供一个基于查询的对象列表)。LINQ/EF是一些了不起的东西,但是一些开发环境还没有达到。net 3.5。
-
数据绑定-从存储过程中获取数据后,将其设置为GridView的数据源,然后调用DataBind方法。GridView控件将自动处理你给它的数据的迭代。如果你需要做一些自定义的列/格式,你可以覆盖RowDataBound事件。
这个过程的每个方面都有很多细微差别和细节,但希望这能让你对开始的事情有基本的了解。
你就快成功了。您需要将选择命令设置为存储过程的名称,然后将SelectCommandType设置为StoredProcedure。然后设置ControlParameter的PropertyName。
这将设置GridView在回发时与文本框的值绑定(单击按钮可以导致回发)
同时设置GridView的DataSourceID以匹配SqlDataSource的ID。
<asp:SqlDataSource ID="Database" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>"
ProviderName="<%$ ConnectionStrings:Database_ConnectionString.ProviderName %>"
SelectCommand="dbo.yourstoredproc" SelectCommandType="StoredProcedure"
OnSelecting="Database_Selecting">
<SelectParameters>
<asp:ControlParameter ControlID="Text1" Name="CustomerID" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
几个指针:
1)使用<asp:textbox id="Text1" runat="server"/>
而不是<input name="CustomerID" id="Text1" type="text" />
-注意runat="server"这允许数据源选择参数找到它的输入源。
2)如果数据确实来自sqldatasource,则将gridview datasourceid指向sqldatasource。否则,哪里是"PersEWD_Database"数据源定义?
3)让您的sqldatasource sql脚本返回所有需要绑定到网格上的字段,而不仅仅是CustomerID。
4)除去空的<script>
块,除非你需要它。
5)去掉OnSelecting="Database_Selecting"
,除非你需要。
6)将<asp:Button> onclick()
更改为onclick="ButtonSearch_Click"
,并在后面的代码中实现ButtonSearch_Click
事件处理程序来重新发布页面。如果您不需要做任何事情,只需要用键入的客户id加载网格,则可以将事件处理程序保留为空。