尝试通过Paramaters和Oledb在文本框中按用户名创建搜索数据库 [英] Trying To Make Search Database by UserName in textbox by Paramaters and Oledb

查看:88
本文介绍了尝试通过Paramaters和Oledb在文本框中按用户名创建搜索数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在使用搜索"按钮,该按钮将按用户名或城市或其他信息搜索数据库中的用户. 我试图对用户名进行参数设置,然后在其中放了一个文本框,如果他在文本框中写了一些东西,然后单击按钮,它将仅显示具有他在文本框内编写的用户名的用户. 但是它不起作用,请帮助我找出如何制作它...

Hey guys i am working on a Search button that will search Users in database by their UserName or Their Cities Or anything else. I Tried to make a paramater of Username That inside i putted a Textbox and if he write something in the Textbox and click On the button it will show only the users with the UserName that he wrote inside the textbox... But its not working guys, Please help me to find out how to make it...

代码:

            <asp:TextBox ID="txtUserNameFooter" runat="server" />
        <asp:Button ID = "SearchButton" Text = "חפש" runat="server" onclientclick="SearchButton_Click" onclick="SearchButton_Click"  />

2 Asp.net(C#)数据库

    protected void SearchButton_Click(object sender, EventArgs e)
{
    using (OleDbConnection sqlCon = new OleDbConnection(connectionStr))
    {
        sqlCon.Open();
        string query = "SELECT * FROM Users WHERE UserName=@UserName";
        OleDbCommand sqlCmd = new OleDbCommand(query, sqlCon);
        sqlCmd.Parameters.AddWithValue("@UserName", txtUserNameFooter.Text);
        sqlCmd.ExecuteNonQuery();
        PopulateGridView();
        sqlCon.Close();
    }
}

3 PopulateGridView代码

 void PopulateGridView()
{
    DataTable dtbl = new DataTable();
    using (OleDbConnection sqlCon = new OleDbConnection(connectionStr))
    {
        sqlCon.Open();
        OleDbDataAdapter sqlDa = new OleDbDataAdapter("SELECT * FROM Users", sqlCon);
        sqlDa.Fill(dtbl);
    }
    if (dtbl.Rows.Count > 0)
    {
        AdminBook.DataSource = dtbl;
        AdminBook.DataBind();
    }
    else
    {
        dtbl.Rows.Add(dtbl.NewRow());
        AdminBook.DataSource = dtbl;
        AdminBook.DataBind();
        AdminBook.Rows[0].Cells.Clear();
        AdminBook.Rows[0].Cells.Add(new TableCell());
        AdminBook.Rows[0].Cells[0].ColumnSpan = dtbl.Columns.Count;
        AdminBook.Rows[0].Cells[0].Text = "No Data Found";
        AdminBook.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
    }
}

网站图片 单击此处查找

我希望它在您在文本框中编写并按下按钮时生成,它将搜索他们的用户名是否类似于文本框,如果有,则仅显示该用户名的数据... 请大家帮我,我将非常高兴获得帮助:)

I want it to make when you write in textbox and press on button it will Search if their a username called like the textbox and then if it have it will show only the data of this username... Please guys help me i will be really happy to get help :)

推荐答案

更新PopulateGridView方法以接受这样的输入参数:

Update PopulateGridView method to accept an input parameter like this:

void PopulateGridView(string searchTerm)
{
    DataTable dtbl = new DataTable();
    using (OleDbConnection sqlCon = new OleDbConnection(connectionStr))
    {

        string query = "SELECT * FROM Users";

       OleDbCommand sqlCmd = new OleDbCommand(query, sqlCon);

       if(!String.IsNullOrWhiteSpace(searchTerm)) {
                 query+=" WHERE UserName=@UserName";
                 sqlCmd.CommandText = query;
                sqlCmd.Parameters.AddWithValue("@UserName", searchTerm);
         }

        sqlCon.Open();
        OleDbDataAdapter sqlDa = new OleDbDataAdapter(sqlCmd);

        sqlDa.Fill(dtbl);
    }

    if (dtbl.Rows.Count > 0)
    {
        AdminBook.DataSource = dtbl;
        AdminBook.DataBind();
    }
    else
    {
        dtbl.Rows.Add(dtbl.NewRow());
        AdminBook.DataSource = dtbl;
        AdminBook.DataBind();
        AdminBook.Rows[0].Cells.Clear();
        AdminBook.Rows[0].Cells.Add(new TableCell());
        AdminBook.Rows[0].Cells[0].ColumnSpan = dtbl.Columns.Count;
        AdminBook.Rows[0].Cells[0].Text = "No Data Found";
        AdminBook.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
    }
}

像这样更新SearchButton_Click方法:

Update SearchButton_Click method like this:

protected void SearchButton_Click(object sender, EventArgs e)
{
   PopulateGridView(txtUserNameFooter.Text);        
}

这篇关于尝试通过Paramaters和Oledb在文本框中按用户名创建搜索数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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