我没有在我的sql数据库中运行我的插入查询

时间:2015-10-03 07:31:18

标签: c# mysql asp.net

protected void Button1_Click1(object sender, EventArgs e) {
    try {
        conn.Open();
        string idquery = "select top 1 pid from post order by pid desc ";
        cmd = new SqlCommand(idquery, conn);
        SqlDataReader reader = cmd.ExecuteReader();
        reader.Read();
        string p1id = (reader["pid"].ToString());
        int pid = Convert.ToInt32(p1id);
        TextBox3.Text = (reader["pid"].ToString());
        conn.Close();
        SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);

        string insertQuery = "insert into post (pid ,pidtype ,title ,question,creationdate) values (@pid,@ptype,@title,@question,@cdate)";
        com = new SqlCommand(insertQuery, conn1);
        DateTime now = DateTime.Now;
        ++pid;
        com.Parameters.AddWithValue("@pid", pid);
        com.Parameters.AddWithValue("@ptype", 1);
        com.Parameters.AddWithValue("@title", TextBox2.Text);
        com.Parameters.AddWithValue("@question", TextArea1.Text);
        com.Parameters.AddWithValue("@cdate", now);
        Response.Write("successfull");
        Response.Redirect("questions.aspx");
        conn1.Close();


    } catch (Exception ex) {
        Response.Write("error" + ex.ToString());
    }
}

这是我写的代码..没有给出任何错误或异常... bt数据没有插入数据库

1 个答案:

答案 0 :(得分:0)

您需要在添加所有参数后调用ExecteNonQuery: -

com.ExecteNonQuery();