本文介绍了如何从表中搜索值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从表中搜索值.我有文本框和按钮.当我在文本框中输入字符串并按下按钮时,应显示表中所有匹配的记录.请帮助我.

How to search values from table. I have textbox and button. when i enter string in textbox and press button all matching records from table should display. Please help me.

推荐答案



set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go



ALTER PROCEDURE [dbo].[usp_SearchRoomDetails]
@RoomNo varchar(50)


AS
BEGIN

    SET NOCOUNT ON;


    SELECT * from RoomDetailsTab  where RoomNo=@RoomNo
END





并在页面中将此sp称为






and call this sp in page


protected void Search_Click(object sender, ImageClickEventArgs e)
    {
        string RoomNo = "";
        RoomNo = txtRoomNo.Text;
        
        SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["dbConnection"].ToString());

        cnn.Open();
        SqlCommand cmd = new SqlCommand();

        cmd.Connection = cnn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "usp_SearchRoomDetails";

        SqlParameter pRoomNo = new SqlParameter("@RoomNo", SqlDbType.VarChar, 50);
        pRoomNo.Value = RoomNo;
        cmd.Parameters.Add(pRoomNo);
      


        DataTable dt = new DataTable();

        SqlDataAdapter da = new SqlDataAdapter(cmd);


        da.Fill(dt);


        if (dt.Rows.Count > 0)
        {
//if u want to display searched record in textbox
            txtRNo.Text = dt.Rows[0]["RoomNo"].ToString();
            txtDetails.Text = dt.Rows[0]["Details"].ToString();
           

           
        }
        if (dt.Rows.Count == 0)
        {
//if record is empty will show message
            Response.Write("No result found");
        }
        cnn.Close();
    }





试试这个
快乐的编码.





try this
happy coding..


这篇关于如何从表中搜索值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 00:56