如何在数据库中搜索记录? [英] How to search records in a database?

查看:94
本文介绍了如何在数据库中搜索记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我有一个用于搜索的图像按钮和一个用于显示记录的网格视图。如果记录/ s不存在,页面将显示找到的物品数量。当它找到匹配的记录时,它将显示在我的网格视图中。现在,我的问题是我的搜索功能不起作用。谁能帮我?



这是我搜索的.cs文件:



 < span class =code-keyword> protected   void  ImageButton2_Click( object  sender,ImageClickEventArgs e )
{
repgen_connection();
DataTable dt = new DataTable();
if (sbu.SelectedItem.Text == null && company_code.Text = = null && buyer_name.Text == null && proj_name.Text == < span class =code-keyword> null && unit_desc.Text == null && milestone.SelectedItem.Text == < span class =code-keyword> null && checklist.SelectedItem.Text == null && activity.SelectedItem.Text = = null
{
ScriptManager.RegisterStartupScript( this .GetType(), popup alert('找不到记录!'); true );
}
else if (sbu.SelectedValue!= null || company_code.Text!= null || buyer_name.Text!= null || proj_name.Text!= null || unit_desc.Text!= null || milestone.SelectedValue!= null || checklist.SelectedValue!= null || activity.SelectedValue!= < span class =code-keyword> null

{
string strQuery = 选择DISTINCT品牌,company_code,Contract_num,buyer_name,proj_name,unit_description,里程碑,清单,活动,状态,logged_by,date_created,completed_by,date_completed FROM rawdata WHERE< /跨度>;
if (sbu.SelectedValue!=
{strQuery + = 品牌LIKE'% + sbu .SelectedValue + %'AND; }
if (company_code.Text!=
{strQuery + = company_code LIKE'% + company_code.Text + %'AND; }
if (buyer_name.Text!=
{strQuery + = buyer_name LIKE'% + buyer_name.Text + %'AND; }
if (proj_name.Text!=
{strQuery + = proj_name LIKE'% + proj_name.Text + %'AND; }
if (unit_desc.Text!=
{strQuery + = unit_description LIKE'% + unit_desc.Text + %'AND; }
if (milestone.SelectedValue!=
{strQuery + = 里程碑LIKE'% + milestone.SelectedValue + %'AND; }
if (checklist.SelectedValue!=
{strQuery + = checklist LIKE'% + checklist.SelectedValue + %'AND; }
if (activity.SelectedValue!=
{strQuery + = activity LIKE'% + activity.SelectedValue + %'AND; }
strQuery + = proj_name和unit_description AND company_code AND status ='not completed'ORDER BY Contract_num DESC ;

SqlCommand query = new SqlCommand(strQuery);
SqlDataAdapter sda = new SqlDataAdapter();
query.CommandType = CommandType.Text;
query.Connection = amicassaCon_repgen;

尝试
{
amicassaCon_repgen.Open();
sda.SelectCommand = query;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (例外情况)
{
// Response.Write(ex.Message);
}
finally
{
amicassaCon_repgen.Close();
sda.Dispose();
amicassaCon_repgen.Dispose();
// Response.Write(strQuery);
}
}


int tot = 0 ;
foreach (DataRow dr in dt.Rows)
{
tot = tot + 1 ;
}
itms.Text = 找到的项目: + tot + < span class =code-string>
items;
}

解决方案

更改

 strQuery + = < span class =code-string>  proj_name和unit_description AND company_code AND status ='not completed'Order by Contract_num DESC; 



to

 strQuery + =   proj_name ='not completed'和unit_description ='not completed'AND company_code ='not completed'AND status ='not completed'Order by Contract_num DESC; 


我认为你会得到以下错误因为

 strQuery + =    proj_name和unit_description AND company_code AND status ='not completed'Order by Contract_num DESC; 





错误:

在预期条件的上下文中指定的非布尔类型的表达式,靠近'和'。



tel你想用上面的行做什么,以便我们可以告诉你该怎么做


< blockquote>在下面的链接中查看完整示例。这将帮助您满足您的要求。



http://www.c-sharpcorner.com/UploadFile/0c1bb2/searching-records-from-database-and-display-in-gridview-usin/ [ ^ ]


Hi guys!

I have an image button for searching and a grid view for displaying records.If the record/s does not exist, the page will display the number of items found. And when it found a record that matches, it will display in my grid view. Now, my problem here is my search function does not work. Can anyone help me?

Here is my .cs file for Searching:

protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        repgen_connection();
        DataTable dt = new DataTable();
        if (sbu.SelectedItem.Text == null && company_code.Text == null && buyer_name.Text == null && proj_name.Text == null && unit_desc.Text == null && milestone.SelectedItem.Text == null && checklist.SelectedItem.Text == null && activity.SelectedItem.Text == null)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('No records found!');", true);
        }
        else if (sbu.SelectedValue != null || company_code.Text != null || buyer_name.Text != null || proj_name.Text != null || unit_desc.Text != null || milestone.SelectedValue != null || checklist.SelectedValue != null || activity.SelectedValue != null)
        {
            string strQuery = "Select DISTINCT brand, company_code,Contract_num,buyer_name, proj_name ,unit_description, milestone, checklist,activity, status,logged_by,date_created,completed_by,date_completed FROM  rawdata WHERE ";
            if (sbu.SelectedValue != "")
            { strQuery += " brand LIKE '%" + sbu.SelectedValue + "%' AND"; }
            if (company_code.Text != "")
            { strQuery += " company_code LIKE '%" + company_code.Text + "%' AND"; }
            if (buyer_name.Text != "")
            { strQuery += " buyer_name LIKE '%" + buyer_name.Text + "%' AND"; }
            if (proj_name.Text != "")
            { strQuery += " proj_name LIKE '%" + proj_name.Text + "%' AND"; }
            if (unit_desc.Text != "")
            { strQuery += " unit_description LIKE '%" + unit_desc.Text + "%' AND"; }
            if (milestone.SelectedValue != "")
            { strQuery += " milestone LIKE '%" + milestone.SelectedValue + "%' AND"; }
            if (checklist.SelectedValue != "")
            { strQuery += " checklist LIKE '%" + checklist.SelectedValue + "%' AND"; }
            if (activity.SelectedValue != "")
            { strQuery += " activity LIKE '%" + activity.SelectedValue + "%' AND"; }
            strQuery += " proj_name and unit_description AND company_code  AND status='not completed' ORDER BY Contract_num DESC";

            SqlCommand query = new SqlCommand(strQuery);
            SqlDataAdapter sda = new SqlDataAdapter();
            query.CommandType = CommandType.Text;
            query.Connection = amicassaCon_repgen;

            try
            {
                amicassaCon_repgen.Open();
                sda.SelectCommand = query;
                sda.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                // Response.Write(ex.Message);
            }
            finally
            {
                amicassaCon_repgen.Close();
                sda.Dispose();
                amicassaCon_repgen.Dispose();
                //Response.Write(strQuery);
            }
        }
       

        int tot = 0;
        foreach (DataRow dr in dt.Rows)
        {
            tot = tot + 1;
        }
        itms.Text = "Item's found: " + tot + " items";
    }

解决方案

change

strQuery += " proj_name and unit_description AND company_code  AND status='not completed' ORDER BY Contract_num DESC";


to

strQuery += " proj_name = 'not completed' and unit_description ='not completed' AND company_code ='not completed' AND status='not completed' ORDER BY Contract_num DESC";


I think you will get the below error because of

strQuery += " proj_name and unit_description AND company_code  AND status='not completed' ORDER BY Contract_num DESC"; 

line

error:
An expression of non-boolean type specified in a context where a condition is expected, near 'and'.

tel what you want to do with the above line so that we can tell you what to do


See a Full Example in below link. This will help you to fill your requirement.

http://www.c-sharpcorner.com/UploadFile/0c1bb2/searching-records-from-database-and-display-in-gridview-usin/[^]


这篇关于如何在数据库中搜索记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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