必须在诸如名称之类的数据库上搜索查询并吐出任何类似的东西

时间:2018-06-06 06:42:55

标签: c# html sql asp.net odbc

基本上它会在网站上读取数据库文件,然后我想搜索一个名称,然后它会弹出所有结果,就像searchName一样,然后将它作为一个表格输出到网页上,这应该是通过按下按钮触发并至少打了一封信,我已经打了几个小时这可以让任何人给我一些关于我做错了什么的见解?我也在使用Odbc在我的系统上创建一个DNS,这是一个小项目,只需要一些麻烦寻找帮助,因为Odbc代码适用于添加记录和查看所有记录,但在此页面上搞砸了。

干杯,乔希

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data.Odbc" %>

<%
    OdbcConnection oConn = new OdbcConnection("DSN=sailors");
    oConn.Open();

    string str_sql = "select * from MonsterList where MonsterNames like searchName"; // this is an sql string   '*' includes all fields from table

    OdbcCommand oCmd = new OdbcCommand(str_sql, oConn);

    OdbcDataReader oRs = oCmd.ExecuteReader();
%>

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Search Monster</title>
</head>
<body>

  <form method="post" runat="server">

    <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    <strong>Search </strong>
                </td>
                <td>
                    <asp:TextBox ID="searchName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="btnSubmit" runat="server" Text="Search" />
                </td>
            </tr>
    </table>

 </form>
    <table <link rel="displayrecordSS" href="displayrecordSS.css"/> 
        <%
            while(oRs.Read())
            {
                string str_out = @"<tr valign=""top"">" +
                                  "<td>" + oRs["MonsterNames"] + "</td>" +
                                  "<td>" + oRs["Element"] + "</td>" +
                                  "<td>" + oRs["Weakness"] + "</td>" +
                                  "</tr>" +
                                  @"<tr bgcolor=""#ff0000""><td height=""1"" colspan=""3""></td></tr>";

                Response.Write(str_out);

            }

        %>


</body>
</html>

<%
    oRs.Close();
    oRs = null;

    oCmd.Dispose();
    oCmd = null;

    oConn.Close();
    oConn.Dispose();
    oConn = null;
%>

工作添加页面

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.Odbc" %>

<%
    OdbcConnection oConn = new OdbcConnection("DSN=sailors");
    oConn.Open();

    if ((Request["MonsterNames"] != null) && (Request["Element"].ToString() != ""))
    {

        string str_sql = @"insert into MonsterList (MonsterNames,Element, Weakness) values " +
        "('" + Request["MonsterNames"] + "'," +
        "'" + Request["Element"] + "', '" + Request["Weakness"] + "')";

        OdbcCommand oCmd = new OdbcCommand(str_sql, oConn);
        oCmd.ExecuteNonQuery();

        oCmd.Dispose();
        oCmd = null;

        Response.Write("<br><br><br>");
    }


%>


<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Add Monster</title>
</head>
<body>
        <div class="topnav" id="myTopnav">
            <link rel="stylesheet" href="stylesheet.css" />
            <a href="MainPg.aspx" class="active">Home</a>
            <a href="Display Record.aspx">Monster List</a>
            <a href="Search Record.aspx">Search A Monster</a>
        </div>

                                                    <!-- all FORM elements (input, checkbox etc) contains data to submit. start of FORM -->
    <form method="post" runat="server">

        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    <strong>Monster Name:</strong>
                </td>
                <td>
                    <asp:TextBox ID="MonsterNames" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <strong>It's Element:</strong>
                </td>
                <td>
                    <asp:TextBox ID="Element" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <tr>
                <td>
                    <strong>Known Weaknesses (Seperate by '/'):</strong>
                </td>
                <td>
                    <asp:TextBox ID="Weakness" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="btnSubmit" runat="server" Text="Add Record" />
                </td>
            </tr>
        </table>

    </form>


</body>
</html>

<%

    oConn.Close();
    oConn.Dispose();
    oConn = null;
%>

工作显示所有页面

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data.Odbc" %>

<%
    OdbcConnection oConn = new OdbcConnection("DSN=sailors");
    oConn.Open();

     string str_sql = "select * from MonsterList "; // this is an sql string   '*' includes all fields from table

      OdbcCommand oCmd = new OdbcCommand(str_sql, oConn);

    OdbcDataReader oRs = oCmd.ExecuteReader();

%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Monster List</title>
</head>

<body leftmargin="0" rightmargin="0" topmargin="0">
        <div class="topnav" id="myTopnav">
            <link rel="stylesheet" href="stylesheet.css" />
            <a href="MainPg.aspx" class="active">Home</a>
            <a href="Add Monster.aspx">Add Monster</a>
            <a href="Search Record.aspx">Search A Monster</a>
        </div>

    <table <link rel="displayrecordSS" href="displayrecordSS.css"/> 
        <%
            while(oRs.Read())
            {
                string str_out = @"<tr valign=""top"">" +
                                  "<td>" + oRs["MonsterNames"] + "</td>" +
                                  "<td>" + oRs["Element"] + "</td>" +
                                  "<td>" + oRs["Weakness"] + "</td>" +
                                  "</tr>" +
                                  @"<tr bgcolor=""#ff0000""><td height=""1"" colspan=""3""></td></tr>";

                Response.Write(str_out);

            }

        %>



    </table>
</body>
</html>


<%
    oRs.Close();
    oRs = null;

    oCmd.Dispose();
    oCmd = null;

    oConn.Close();
    oConn.Dispose();
    oConn = null;
%>

0 个答案:

没有答案