0

我应该在我的网页上从网络表单接收 POST 数据并存储它。我有以下代码行来使用 PHP 接收它。

$jdata = json_decode($_POST['data'], true);

在 ASP.NET / C# 中这个等价物是什么?

换句话说,我如何接收 POST 数据并使用 C# 对其进行解码?

4

1 回答 1

0

我假设您的下一个问题将是如何使用 ASP.NET 更新数据库,所以我已经在以下代码中回答了这个问题以及这个问题:

using System.Data;
using System.Data.SqlClient;

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Form["Name"] == null) Response.End();
    if (Request.Form["ID"] == null) Response.End();

    //Connection string
    SqlConnection conn = new SqlConnection("Data Source=sqlsvr.net;Initial Catalog=ohsrespirator;Persist Security Info=True;User ID=user;Password=pwd");

    //Set a command
    SqlCommand cmd = new SqlCommand("UPDATE Table_Name SET Column_Name = @value1, Column_ID = @value2", conn);
    cmd.Parameters.Add("@value1", SqlDbType.NVarChar).Value = Request.Form["Name"] as string;
    cmd.Parameters.Add("@value2", SqlDbType.NVarChar).Value = Request.Form["ID"] as string;

    //Open connection and execute update
    try
    {
        conn.Open();
        cmd.ExecuteNonQuery();
    }
    catch { }
    finally { if (conn != null) conn.Close(); }
}

PS 如果您的数据以 JSON 格式输入,您可能还需要这个:

using System.Web.Script.Serialization;

JavaScriptSerializer JSS = new JavaScriptSerializer();
var JSON = JSS.Deserialize<dynamic>(Request.Form["POSTED_JSON"]);
于 2013-10-16T02:54:28.093 回答