我正在开发允许使用对象数据源和详细信息视图插入记录的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();
}
}
}
答案 0 :(得分:0)
ExecuteNonQuery()
返回更新的总行数,因此您使用返回值并显示消息以通知用户。
来自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);
}
答案 1 :(得分:0)
常用方法:
int result = cmd.ExecuteNonQuery();
如果插入操作成功,结果为1(受影响的行数)。