查询系统 - 如何搜索和显示数据?

时间:2013-11-30 05:02:49

标签: c# asp.net

我需要有关如何根据勾选的复选框使搜索功能正常工作的帮助,并在结果页面中显示结果。 基本上,我已准备好数据库,数据库和Web服务之间的连接已经完成,表单也已准备就绪。

以下是我的SearchPage.aspx.cs(主页)

    public partial class SearchPage : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {


    }

    protected void searchbtn_Click(object sender, EventArgs e)
    {
        string searchbox = inputtxt.Text;            

        if (searchbox.Length == 0)
        {
            errormsglbl.ForeColor = Color.Red;
            errormsglbl.Text = "Please fill in the search box below";
        }

        else
        {

            string query = "SELECT * from item_list ";

            //fix this ?input = inputtxt.txt, title = title.......
            //if all are untick change all to true
            if (titlelbl.Checked)
                query += "WHERE Title like '%" + inputtxt.Text + "%'";
            else if (authorlbl.Checked)
                query += "WHERE Author = '%" + inputtxt.Text + "%'";
            else if (publisherlbl.Checked)
                query += "WHERE Publisher = '%" + inputtxt.Text + "%'";

            string connectionString =
                "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source='C:\\Users\\Mister\\Documents\\Visual Studio
2012\\Project\\LibraryQuerySystem\\QuerySystem\\App_data\\items.mdb';";

            string queryString = "INSERT INTO item_list VALUES ('" + Title + "', '" +
Author + "', '" + Publisher + "')";

            using (OleDbConnection connection = new OleDbConnection(connectionString)) 
            {
                connection.Open();
                OleDbConnection commandText = queryString;
                //OleDbCommand command = new OleDbCommand(queryString, connection);
                OleDbDataReader reader = commandText.ExecuteReader();

                while (reader.Read())
                {
                    result+="Title = "+ reader["Title"].ToString()+"  ";
                    result+="Author= "+ reader["Author"].ToString()+"  ";
                    result+="Publisher= "+ reader["Publisher"].ToString()+"  ";
                    result+"<br/>";//dd new line
                }
                reader.Close();
                connection.Close();

                //set Session here
                Session["Results"]=ResultPage;
                //now redirect
                Response.Redirect("~/ResultPage.aspx");
            }

        }

    }

    protected void addbtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("/AddPage.aspx");
    }

    protected void authorlbl_CheckedChanged(object sender, EventArgs e)
    {

    }

    protected void publisherlbl_CheckedChanged(object sender, EventArgs e)
    {

    }

    protected void inputtxt_TextChanged(object sender, EventArgs e)
    {

    }
}

这是我的ResultPage.aspx.cs

public partial class ResultPage : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        results.InnerHtml = Session["Results"].ToString();

    }

    protected void backbtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("/SearchPage.aspx");
    }

}

和我的DatabaseService.asmx.cs

/// <summary>
/// Summary description for Databases
/// </summary>
[WebService(Namespace = "http://tempuri.org/", Name="Library Database",
Description="This is Database of the Library")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment
the following line. 
// [System.Web.Script.Services.ScriptService]
public class Databases : System.Web.Services.WebService
{        
    [WebMethod]
    public string CheckConnection()
    {
            string result = "";
            string connectionString =
                "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source='C:\\Users\\Mister\\Documents\\Visual Studio
2012\\Projects\\LibraryQuerySystem\\QuerySystem\\App_data\\items.mdb';";

        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            try
            {
                connection.Open();
                result = "Connection Success";
            }
            catch (Exception e)
            {
                result = "Error in Connection";
            }
        }
        return result;
    }

    [WebMethod]
    public string addBook(String title, String author, String publisher)
    {
        string result = "";

        string connectionString =
            "Provider=Microsoft.Jet.OLEDB.4.0;" +
            "Data Source='C:\\Users\\Mister\\Documents\\Visual Studio 2012\\Project
\\LibraryQuerySystem\\QuerySystem\\App_data\\items.mdb';";

        string queryString = "INSERT INTO item_list VALUES ('" + title + "', '" +
author + "', '" + publisher + "')";

        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            connection.Open();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = queryString;
            // OleDbCommand command = new OleDbCommand(queryString, connection);

            command.ExecuteNonQuery();

            connection.Close();

            result += "Item added";
        }
        return result;
    }




    [WebMethod]
    public string[] searchBook(string input, Boolean title, Boolean author, Boolean
publisher)
    {

        string connectionString =
            "Provider=Microsoft.Jet.OLEDB.4.0;" +
            "Data Source='C:\\Users\\Mister\\Documents\\Visual Studio 2012\\Project
\\LibraryQuerySystem\\QuerySystem\\App_data\\items.mdb';";

        string queryString = "SELECT * FROM item_list WHERE Title = '" + input + "' OR
Author = '" + input + "' OR Publisher = '" + input + "'";

        string lists = "";

        string[] book = new string[3];

        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            connection.Open();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = queryString;

            OleDbDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
            }

            if (title == true)
            {
                queryString = "SELECT * FROM item_list WHERE Title like '%" + input +
"%'";

                OleDbCommand comd2 = new OleDbCommand(queryString, connection);
                comd2.CommandText = queryString;
                OleDbDataReader read2 = comd2.ExecuteReader();

                while (read2.Read())
                {
                    lists += "<p> title = " + read2[0].ToString() + "<br> author = " +
read2[1].ToString() + "<br> publisher = " + read2[2].ToString() + "</p>";
                }
            }

            if (author == true)
            {
                queryString = "SELECT * FROM item_list WHERE Author LIKE '%" + input +
"%'";

                OleDbCommand comd3 = new OleDbCommand(queryString, connection);
                comd3.CommandText = queryString;
                OleDbDataReader read3 = comd3.ExecuteReader();

                while (read3.Read())
                {
                    lists += "<p> title = " + read3[0].ToString() + "<br> author = " +
read3[1].ToString() + "<br> publisher = " + read3[2].ToString() + "</p>";
                }
            }

            if (publisher == true)
            {
                queryString = "SELECT * FROM item_list WHERE Publisher LIKE '%" + input +
"%'";

                OleDbCommand comd4 = new OleDbCommand(queryString, connection);
                comd4.CommandText = queryString;
                OleDbDataReader read4 = comd4.ExecuteReader();

                while (read4.Read())
                {
                    lists += "<p> title = " + read4[0].ToString() + "<br> author = " +
read4[1].ToString() + "<br> publisher = " + read4[2].ToString() + "</p>";
                }
            }

            reader.Close();
            connection.Close();
        }
        return book;
    }
}

我遇到的问题出现在搜索页面中,其中包含以下几行:

2.当前上下文中不存在名称“作者” 3.“发布者”这个名称在当前上下文中不存在

            string queryString = "INSERT INTO item_list VALUES ('" + Title + "', '" +
Author + "', '" + Publisher + "')";

4.Cannot隐式将类型'string'转换为'System.Data.OleDbConnection' 5.'System.Data.OleDbConnection'不包含“ExecuteReader”的定义,并且没有扩展方法'ExecuteReader'接受类型'System.Data.OleDb.OleDbConnection'的第一个参数可以找到

            using (OleDbConnection connection = new OleDbConnection(connectionString)) 
            {
                connection.Open();
                OleDbConnection commandText = queryString;
                //OleDbCommand command = new OleDbCommand(queryString, connection);
                OleDbDataReader reader = commandText.ExecuteReader();

6.当前上下文中不存在名称“结果” 9.Only赋值,调用,递增,递减,等待和新对象表达式可用作语句

while (reader.Read())
                {
                    result+="Title = " reader["Title"].ToString()+"  ";
                    result+="Author= " reader["Author"].ToString()+"  ";
                    result+="Publisher= " reader["Publisher"].ToString()+"  ";
                    result+"<br/>";//dd new line
                }

请帮我找到解决方案。

1 个答案:

答案 0 :(得分:1)

根据我的理解:

1.您有一个输入TextBox来阅读input String基本上它是一个搜索字符串。
2.你有3 CheckBox个控件 他们是:

a.Author
b.Publisher
c.Title

3.基于用户选择,必须使用给定的输入字符串触发查询。

示例:如果用户想要搜索Title,则必须在输入some titleTextBox select/Check输入Title CheckBox Books查找给定Title的{​​{1}} PublisherAuthor

相同

尝试此代码:(示例代码)

设计代码:

<asp:CheckBox ID="chkTitle" runat="server" Text="Title"/>
    <asp:CheckBox ID="chkAuthor" runat="server" Text="Author" />
    <asp:CheckBox ID="chkPublisher" runat="server" Text="Publisher" />
      <asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click1" />

代码背后:

    protected void Button1_Click1(object sender, EventArgs e)
        {

            String query = "SELECT * FROM item_list ";

            if(chkTitle.Checked)
            query+="WHERE Title like '%" + txtInput.Text + "%'";
            else if(chkAuthor.Checked)
            query+="WHERE Author = '%" + txtInput.Text + "%'";
            else if(chkPublisher.Checked)
            query+="WHERE Publisher = '%" + txtInput.Text + "%'";

            //now execute command with  query variable to perform search 
string connectionString =
            "Provider=Microsoft.Jet.OLEDB.4.0;" +
            "Data Source='C:\\Users\\Mister\\Documents\\Visual Studio 2012\\Project
\\LibraryQuerySystem\\QuerySystem\\App_data\\items.mdb';";

        string queryString = "INSERT INTO item_list VALUES ('" + title + "', '" +
author + "', '" + publisher + "')";

        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            connection.Open();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = queryString;
            // OleDbCommand command = new OleDbCommand(queryString, connection);
            OleDbDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
              result+="Title = "+ reader["Title"].ToString()+"  ";
              result+="Author= "+ reader["Author"].ToString()+"  ";
              result+="Publisher= "+ reader["Publisher"].ToString()+"  ";
              result+"<br/>";//dd new line
            }
            connection.Close();

           //set Session here
           Session["Results"]=result;
           //now redirect
           Response.Redirect("~/SearchPage.aspx");
        }

        }

现在SearchPage.aspx页面应包含以下代码:

SearchPage.aspx的设计代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SearchPage.aspx.cs" Inherits="SearchPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="results" runat="server">

    </div>
    </form>
</body>
</html>

SearchPage页面背后的代码

Page_Load应将结果添加到div元素

public partial class SearchPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        results.InnerHtml = Session["Results"].ToString();
    }
}

解决方案3:您需要用于组合字符串的连接运算符+

试试这个:

while (reader.Read())
                {
                    result+="Title = "+ reader["Title"].ToString()+"  ";
                    result+="Author= "+ reader["Author"].ToString()+"  ";
                    result+="Publisher= "+ reader["Publisher"].ToString()+"  ";
                    result+"<br/>";//dd new line
                }

注意:由于这是一个示例代码,我希望您可以根据您的要求更改代码,如果您需要其他内容,请告诉我。