使用存储过程创建搜索功能

时间:2013-05-13 03:32:22

标签: 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>

和一个图像按钮:

<asp:ImageButton ID="BtnSearch" runat="server" ImageUrl="../Support/Image/MagnifierGlass.png" Width="75%" Height="75%" OnClientClick="openNewWin();return false;" />

和gridview:

<asp:Panel ID="PanelDGV" runat="server" Height="100%" ScrollBars="None" Width="100%">
  <asp:GridView ID="DGV" runat="server" AutoGenerateColumns="False" GridLines="None" AllowPaging="true" PageSize="2" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
      <Columns>
  <asp:BoundField DataField="ProjectCode" HeaderText="Project Code" />
  <asp:BoundField DataField="ProjectName" HeaderText="Project Name" />
  <asp:ButtonField ButtonType="Image" ImageUrl="../Support/Image/Edit.png" ItemStyle-HorizontalAlign="Center" CommandName="CmdSearch" HeaderText="Edit">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
       </asp:ButtonField>
          </Columns>
             <PagerStyle CssClass="pgr"></PagerStyle>
                <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
                    </asp:GridView>
                       </asp:Panel>

我正在使用带有查询的存储过程(我从另一个数据库获取值,注意Master中的双点.. [MS_Project]):

SELECT [projectCode],[projectName]
  FROM Master..[MS_Project]
  WHERE [projectCode] like '%' + @ProjectCode + '%'
  ORDER BY [projectCode] ASC

我想创建一个搜索功能,所以用户在文本框中键入他们想要的项目代码,然后单击图像按钮,然后搜索结果应该显示在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

结束班

1 个答案:

答案 0 :(得分:0)

这是你可以做的:

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查询检索到的新DataSource重新绑定了SELECT

  

VB.NET

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

试试这个vb代码。我使用转换工具来获取此信息。也许你可能需要在这里进行一些修改。

发表评论:

在页面上添加此行。

public string CfgConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("CfgConnectionString‌​").ConnectionString;

并改变你的第一行:

Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("CfgConnectionString").ToString())

你也错过了

Dim ds As New DataSet()