我在这里为家庭作业创建了一个方法。我相信它有效,我想测试它是否有效。所以方法如下:
public static bool UpdatePerson (Personnel person, out string result)
{
result = "update not successful";
bool flag = false;
System.Data.SqlClient.SqlCommand updatePerson = new System.Data.SqlClient.SqlCommand();
updatePerson.Connection = Data.con;
//updatePerson.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("perFirstName", person.First);
SqlParameter p2 = new SqlParameter("perMiddleName", person.Middle);
SqlParameter p3 = new SqlParameter("perLastName", person.Last);
SqlParameter p4 = new SqlParameter("ID", person.PersonnelID);
updatePerson.Parameters.Add(p1);
updatePerson.Parameters.Add(p2);
updatePerson.Parameters.Add(p3);
updatePerson.Parameters.Add(p4);
updatePerson.CommandText = "Update tblPersonnel Set perFirstName = " + p1 + " perMiddleName = " + p2 + " perLastName = " + p3 + "Where ID = " + p4;
try
{
Data.con.Open();
updatePerson.ExecuteNonQuery();
result = "Update Successful";
flag = true;
}
catch (Exception ex)
{
result = ex.Message;
}
finally
{
if (Data.con.State == System.Data.ConnectionState.Open)
Data.con.Close();
}
return flag;
}
现在这里是测试编码:
using MovieLibrary;
namespace Test
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string result = " ";
MovieLibrary.Personnel update = MovieLibrary.Personnel.UpdatePerson(MovieLibrary.Personnel, out result);
if (update != null)
this.Label1.Text = result;
}
}
唯一给我的问题是在参数中插入数据。这给了我一个错误,说 MovieLibrary.Personnel 是一种类型,不属于那里。