应在运行时动态接受值,而不是初始化的值。如何让它在运行时动态存储?

时间:2017-11-24 06:01:07

标签: c# asp.net angularjs json postgresql

我是AngularJS,C#和JSON的新手。强制要求变量初始化,以便将方法参数传递给C#中的另一个方法。当我调用Save方法时,它要求初始化。初始化后,初始化值仅在我运行时存储。应在运行时动态接受值,而不是初始化的值。如何让它在运行时动态存储?在数据库中,它显示执行后在此处初始化的值。现在我想要的是,我在运行时输入的数据,应该存储这些数据。有两天我搞砸了这段代码。有谁能帮我这个代码?

class StudentInfo
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
}

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string data = "";
        if (Request.InputStream.Length > 0)
        {
            using (var reader = new System.IO.StreamReader(Request.InputStream))
            {
                data = reader.ReadToEnd();
            }

            if (data.Length > 0)
            {
                StudentInfo studentInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<StudentInfo>(data);

                Response.Clear();
                Response.Write("hai");
                int StudentID = 3;   // When i give 2, it will save as 2 in DB
                string StudentName = "karthik";
                Save (StudentID, StudentName);  // How to pass this method to below method , but values should be accepted dynamically at Run time, not the values that is initialized.

            }
        }
    }

    public static void Save(int StudentID, string StudentName)
    {
        using (NpgsqlConnection con = new NpgsqlConnection(@"Server=10;Port=5432;Database=TEST_DB;"))
        {
            using (NpgsqlCommand cmd = new NpgsqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandText = "insert into student1(StudentID,StudentName)values(@StudentID,@StudentName);";
                cmd.Parameters.AddWithValue("@StudentID", StudentID);
                cmd.Parameters.AddWithValue("@StudentName", StudentName);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
}

0 个答案:

没有答案