【问题标题】:SQL Server entry using URL querystring使用 URL 查询字符串的 SQL Server 条目
【发布时间】:2013-10-12 11:51:51
【问题描述】:

我正在使用 PIC 32 控制器的 post 功能获取 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 平台获得这些数据。

我希望我已经说清楚了。

【问题讨论】:

    标签: asp.net sql-server query-string pic32


    【解决方案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);
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 2017-02-06
      相关资源
      最近更新 更多