当插入操作成功时通知用户
本文关键字:通知 用户 成功 插入 操作 | 更新日期: 2023-09-27 17:50:32
我正在开发asp.net应用程序,允许使用对象数据源和详细信息视图插入记录。我想知道是否有任何方法通知用户(例如弹出消息),记录已成功添加?例如,类似的东西,但与插入(我应该使用哪个事件)?
<asp:LinkButton ID="LinkBtnDelete" runat="server"
OnClientClick="if (!confirm('Are you sure you want delete?')) return false;"
CommandName="Delete">Delete
</asp:LinkButton>
插入代码:
public static void InsertPatient(PatientBLL patient)
{
string ins = "INSERT INTO Patients (PPS,Surname,Name, DOB,Gender,BloodGroup,MedicalCard,AddressLine1,AddressLine2,City,County,Phone,Mobile,Email) VALUES(@PPS,@Surname,@Name,@DOB,@Gender,@BloodGroup,@MedicalCard,@AddressLine1,@AddressLine2,@City,@County,@Phone,@Mobile,@Email)";
using (SqlConnection con = new SqlConnection(GetConnectionString()))
{
using (SqlCommand cmd = new SqlCommand(ins, con))
{
cmd.Parameters.AddWithValue("PPS", patient.PPS);
cmd.Parameters.AddWithValue("Surname", patient.Surname);
cmd.Parameters.AddWithValue("Name", patient.Name);
cmd.Parameters.AddWithValue("DOB", patient.DOB);
cmd.Parameters.AddWithValue("Gender", patient.Gender);
cmd.Parameters.AddWithValue("BloodGroup", patient.BloodGroup ?? Convert.DBNull); // if textbox is left empty, use null as column value
cmd.Parameters.AddWithValue("MedicalCard", patient.MedicalCard ); // ?? Convert.DBNull
cmd.Parameters.AddWithValue("AddressLine1", patient.AddressLine1);
cmd.Parameters.AddWithValue("AddressLine2", patient.AddressLine2 ?? Convert.DBNull);
cmd.Parameters.AddWithValue("City", patient.City);
cmd.Parameters.AddWithValue("County", patient.County);
cmd.Parameters.AddWithValue("Phone", patient.Phone ?? Convert.DBNull);
cmd.Parameters.AddWithValue("Mobile", patient.Mobile ?? Convert.DBNull);
cmd.Parameters.AddWithValue("Email", patient.Email ?? Convert.DBNull);
con.Open();
cmd.ExecuteNonQuery();
}
}
}
常用方法:
int result = cmd.ExecuteNonQuery();
如果插入操作成功,结果为1(受影响的行数)。
ExecuteNonQuery()
返回更新的行总数,因此您使用返回值并显示消息来通知用户。
From MSDN: ExecuteNonQuery()
对连接执行一个Transact-SQL语句,并返回
int status = 0;
status = cmd.ExecuteNonQuery();
string script = "alert('"Rows Inserted Successfully!'");";
if(status > 0)
{
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", script, true);
}