這裏是我的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 –