1

我正在使用它的 post 功能从 PIC 32 控制器获取一个 URL。

就像 www.example.com/Default.aspx?x=12&y=23

我想要做的是,当我获得 URL 时,我想将 x 和 y 的值存储到 SQL Server 中。

我在我的系统上使用 IIS 服务器启动了一个 .aspx。aspx页面的编码是..

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
    <title>Sample Configuration Page</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="style1">
            <tr>
                <td>IP Address:</td>
                <td>
                    <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>MAC Address:</td>
                <td>
                    <asp:TextBox ID="TxtUserName" runat="server"></asp:TextBox>
                </td>
            </tr>

        </table>
    </div>
    <asp:Button ID="Button1" runat="server" Text="Save" 
                             onclick="Button1_Click" />
    </form>
</body>
</html>

请指导我。

数据将使用以下 C# 代码存储在数据库中,

     SqlConnection conn = new SqlConnection(GetConnectionString());
            string sql = "INSERT INTO tblRegistration1 (IP, MAC) VALUES "
                        + " (@IP_Address,@MAC_Address)";

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter[] param = new SqlParameter[6];
                //param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
                param[0] = new SqlParameter("@IP_Address", SqlDbType.VarChar, 50);
                param[1] = new SqlParameter("@MAC_Address", SqlDbType.VarChar, 50);

                param[0].Value = IP_Address;
                param[1].Value = MAC_Address;

                for (int i = 0; i < param.Length; i++)
                {
                    cmd.Parameters.Add(param[i]);
                }

                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Insert Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                conn.Close();
            }
 public string GetConnectionString()
    {
        //sets the connection string from your web config file "ConnString" is the name of your Connection String
        return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
    }

与计算机浏览器一起使用时一切正常。但是来自 PIC32 的帖子没有执行。我不知道如何处理返回到包含 IP 和 MAC 详细信息的 IIS 服务器的 URL。在我的应用程序中,我也从计算机中获得了来自 PIC32 平台的这些数据。

我希望我已经把自己说清楚了。

4

1 回答 1

1

要获取可变形式的 URL,请使用

Request.QueryString["variable"];

所以可以使用SqlDataSource来建立SQL Connection或者SqlConnection

SqlDataSource

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:Conn %>'InsertCommand="INSERT INTO [TableName] ([x], [y]) VALUES (@x, @y)" >
            <InsertParameters>
                <asp:QueryStringParameter Name="x" Type="Int16"></asp:Parameter>
                <asp:QueryStringParameter Name="y" Type="Int16"></asp:Parameter>
            </InsertParameters>
</asp:SqlDataSource>

SqlConnection

public void insertData()
        {
            using (SqlConnection con = new SqlConnection(conString))
            {
                con.Open();
                try
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO TableName(x, y) VALUES(@x, @y)", con))
                    {
                        cmd.Parameters.Add(new SqlParameter("x", Request.QueryString["x"]));
                        cmd.Parameters.Add(new SqlParameter("y", Request.QueryString["y"]));
                        cmd.ExecuteNonQuery();
                    }
                }
                catch (Exception Ex)
                {
                    Console.WriteLine("Unable To Save Data. Error - " + Ex.Message);
                }
            }
        }
于 2013-10-12T13:28:15.413 回答