是否可以根据文本框的值从数据库中选择所有可能的记录 [英] Is it possible to select all possible records from database base on the value of textbox

查看:65
本文介绍了是否可以根据文本框的值从数据库中选择所有可能的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法根据文本框的值从数据库表中选择可能的记录并显示数据库中的所有选定记录?



例如,1)如果我在文本框中键入s,则会选择sephiroth,selth and spith并显示在gridview上。 2)如果我在文本框中键入se,则sephiroth和selth将被选中并显示在gridview上。



[PrimaryKey] [name __]

[1_________] [sephiroth]

[2_________] [selth_____]

[3_________] [spith_____]



我相信这可以通过在我的sql语句中使用LIKE来实现,但我只能做第一个选择所有以s开头的记录,但第二个,我完全没有线索怎么办我会非常感谢你的回复。



我尝试过的事情:



在我的标记上我有这个:



Is there a way to select possible record from database table base on the value of textbox and show all selected records on database?

For example, 1) if I type "s" on textbox then "sephiroth, selth and spith" will be selected and be shown on gridview. 2) if I type "se" on textbox then "sephiroth and selth will be selected and be shown on gridview.

[PrimaryKey] [name___]
[1_________] [sephiroth]
[2_________] [selth_____]
[3_________] [spith_____]

I believe this can be achieve by using LIKE on my sql statement but I can only do the first which is selecting all records that begin with "s" but the second one, I totally don't have clue how to do it. I'll be very grateful to your replies.

What I have tried:

On my markup I have this:

<asp:TextBox ID="txtsearchnow" AutoPostBack="true" OnTextChanged="text_search" runat="server" placeholder="Search"></asp:Label>



我将文本框设置为autopostback = true textchanged fire上的函数



我的代码背后:


I make my textbox set to autopostback = true to make the function on textchanged fire

On my code behind:

protected void text_search(object sender, EventArgs e)
{
using(MySqlConnection con = new MySqlConnection(connstring))
{
MySqlCommand com = new MySqlCommand("SELECT * FROM test1 WHERE name=@name",con);
com.Parameters.AddWithValue("@name",txtsearchnow.Text);
MySqlDataAdapter ad = new MySqlDataAdapter(com);
DataTable dt = new DataTable();
dt.clear();
ad.Fill(dt);
gridsearch.DataSource = dt;
gridsearch.DataBind();
ad.Dispose();
}



我第一次尝试恢复了我的代码。


I reverted my code from my first attempt.

推荐答案

你好,



关于后端,这将是你的种子/参数:@ SearchValue varchar(#)



您可以尝试以下任一方法:



使用外卡:



Hi there,

Regarding the back end, this would be your seed/parameter: @SearchValue varchar(#)

You can try either of the following:

Using wild-card:

--At the end
Select Name From YourTable Where name Like @SearchValue + '%'

--At the beginning:
Select Name From YourTable Where name Like '%' + @SearchValue

--both sides: (depending on your scenario, this one could be more useful)
Select Name From YourTable Where name Like '%' + @SearchValue + '%'





使用CHARINDEX函数:(从SQL 2008开始)





Using the CHARINDEX function: (from SQL 2008 onward)

Select Name From YourTable Where CHARINDEX(ISNULL(@IDNum, MR_IdNum), MR_IdNum) > 0





关于前端:

我个人不会将文本框配置为:AutoPostBack =trueOnTextChanged =...,因为我认为这种行为对于大多数基于Web的应用来说并不理想;往返太多了! (再次,这是我个人的意见)。



我会使用javascript或面板配置为使用默认隐藏按钮,该按钮将触发查询并填充网格。如果您实现 updatepanel (ajax),它看起来会更好。



干杯!



Regarding the front-end:
I would personally not configure a textbox as: AutoPostBack="true" OnTextChanged="...", for I believe such behavior is nor ideal for most web based apps; too many round trips! (again, this is my personal opinion).

I would intercept the ENTER key by either using javascript or a panel configured to use a default hidden button, which will trigger the query and populate the grid. It would look even better if you implement an updatepanel (ajax).

Cheers!


试试这个

删除Textbox.text更改

添加一个按钮并尝试这个..

Try this
Remove Textbox.text changed
Add One Button And Try this..
protected void butgo_Click(object sender, EventArgs e)
       {

if (txtname.Text.Trim() != "")
           {
               str = "and name like '%" + txtname.Text.Trim() + "%'";
           }
   string query = "SELECT * FROM test1    where name<>'' " + str + " ";
           SqlCommand cmd = new SqlCommand(query, con);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
           if (dt.Rows.Count != 0)
           {
               Gridview1.DataSource = dt;
              Gridview1.DataBind();
           }
           else
           {

               txtname.Text = "";
               bind();
               Response.Write("<script>No Record Found</script>");
           }}


这篇关于是否可以根据文本框的值从数据库中选择所有可能的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆