这里是我的ASP代码的HTML表格的形式显示数据库表如何将新行添加到现有的表中的HTML
if (operation == "display"){
MySqlDataAdapter da = new MySqlDataAdapter("select StudentId, StudentName from tblStudent where StudentStatus = 'a'", con);
DataTable dt = new DataTable();
da.Fill(dt);
Response.Write("<Table cellpadding = 5>");
Response.Write("<tr>");
Response.Write("<th>");
Response.Write("Student Id");
Response.Write("</th>");
Response.Write("<th>");
Response.Write("Student Name");
Response.Write("</th>");
Response.Write("</tr>");
foreach (DataRow dr in dt.Rows){
Response.Write("<tr>");
Response.Write("<td>");
Response.Write(dr["StudentId"].ToString());
Response.Write("</td>");
Response.Write("<td>");
Response.Write(dr["studentName"].ToString());
Response.Write("</td>");
Response.Write("</tr>");
}
Response.Write("</Table>");
}
这里是我的插入代码:
if (operation == "insert"){
string sid, sname;
sid = Request.QueryString["StudentId"].ToString();
sname = Request.QueryString["StudentName"].ToString();
//MySqlDataAdapter da = new MySqlDataAdapter("insert into tblStudent (StudentId, StudentName) values (sid, sname)", con);
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into tblStudent (StudentId, StudentName) values('" + sid.ToString() + "', '" + sname.ToString() + "')";
cmd.ExecuteNonQuery();
con.Close();
}
这是完美地工作。但是当我执行插入操作时,我不想再读整个表格。我只想将行附加到在cmd.ExecuteNonQuery返回1时显示方法中定义的HTML表中(以确保数据库表已更新)。怎么样?
如果你不想重新加载整个数据,你应该在客户端进行插入操作。 –
使用参数化查询以避免SQL注入攻击 – Sankar
在ajax中发生的一切,没有重新加载点@NumanKIZILIRMAK –