Asp.net 使用存储过程创建搜索函数
假设我有ProjectCode文本框:Asp.net 使用存储过程创建搜索函数,asp.net,vb.net,sql-server-2008,search,stored-procedures,Asp.net,Vb.net,Sql Server 2008,Search,Stored Procedures,假设我有ProjectCode文本框: <td align="left" width="200px"> <asp:TextBox ID="TbProjectCode" runat="server" Width="194px"></asp:TextBox> </td> 我想创建一个搜索功能,这样用户就可以在文本框中键入他们想要的项目代码,然后单击imagebutton,然后搜索结果应该显示在gridview中,还有其他方法吗?多谢各位 编辑 我
<td align="left" width="200px">
<asp:TextBox ID="TbProjectCode" runat="server" Width="194px"></asp:TextBox>
</td>
我想创建一个搜索功能,这样用户就可以在文本框中键入他们想要的项目代码,然后单击imagebutton,然后搜索结果应该显示在gridview中,还有其他方法吗?多谢各位
编辑
我在.vb中添加:
Protected Sub BtnSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles BtnSearch.Click
Dim ds As New DataSet()
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("CfgConnectionString").ToString())
Using command As New SqlCommand()
command.CommandType = CommandType.StoredProcedure
command.CommandText = "msProject_Select"
command.Connection = connection
command.Parameters.AddWithValue("@ProjectCode", TbProjectCode.Text)
connection.Open()
Dim a As New SqlDataAdapter(command)
a.Fill(ds)
End Using
End Using
DGV2.DataSource = ds
DGV2.DataBind()
End Sub
最终课程这是您可以做的:
protected void BtnSearch_Click(object sender, ImageClickEventArgs e)
{
DataSet ds = new DataSet();
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionString"].ToString()))
{
using (SqlCommand command = new SqlCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "youProcedureName";
command.Connection = connection;
command.Parameters.AddWithValue("@ProjectCode", TbProjectCode.Text);
connection.Open();
SqlDataAdapter a = new SqlDataAdapter(command);
a.Fill(ds);
}
}
DGV.DataSource = ds;
DGV.DataBind();
}
这个想法很简单。在搜索(单击)事件中,您正在使用新的数据源重新绑定您的GridView
,该数据源是您通过新的选择
查询检索到的
VB.NET
试试这个vb
code。我已经使用转换工具得到这个。也许你需要在这里做一些修改
以下是您的评论:
在页面上添加这一行
public string CfgConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("CfgConnectionString").ConnectionString;
然后像这样更改您的第一行:
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("CfgConnectionString").ToString())
你也失踪了
Dim ds As New DataSet()
你面临的问题是什么…标记和查询看起来很完美?你想要.cs代码吗?@PraveenNambiar也许我需要隐藏的代码或者其他需要的代码,你能帮我吗?顺便说一下,我正在使用VB。我会给你.cs代码。使用转换工具r转换它,至少你会有一个想法。谢谢你,请在回答中发布它,这样我可以接受它,如果它工作我将数据集编辑到我的数据集:DsPlaygroundProject,并将连接字符串更改为CF.CfgConnectionString,过程名称更改为msProject\u Select,它现在仍在工作,将您在问题中所做的操作的代码发布…一定是某个小错误。顺便说一句,我有一个CF.vb,其中包含以下代码Public CfgConnectionString作为String=System.Configuration.ConfigurationManager.ConnectionStrings(“CfgConnectionString”).ConnectionString
这就是我在连接字符串中输入CF.CfgConnectionString
的原因。我看到代码u已发布,它无法工作。请参阅我答案底部的更新。我应该将公共字符串CfgConnectionString=System.Configuration.ConfigurationManager.ConnectionStrings(“CfgConnectionString”)放在哪里").ConnectionString;
?在.vb中的aspx页中?
Protected Sub BtnSearch_Click(sender As Object, e As ImageClickEventArgs)
Dim ds As New DataSet()
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("YourConnectionString").ToString())
Using command As New SqlCommand()
command.CommandType = CommandType.StoredProcedure
command.CommandText = "youProcedureName"
command.Connection = connection
command.Parameters.AddWithValue("@ProjectCode", TbProjectCode.Text)
connection.Open()
Dim a As New SqlDataAdapter(command)
a.Fill(ds)
End Using
End Using
DGV.DataSource = ds
DGV.DataBind()
End Sub
public string CfgConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("CfgConnectionString").ConnectionString;
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("CfgConnectionString").ToString())
Dim ds As New DataSet()