我想知道下面示例中的删除行ds.Tables[0].Rows[1].Delete();
。我想在第二行删除删除命令与脚本"DELETE FROM Person WHERE PersonId = @PersonId"
和参数@PersonId
一起使用。但传递给@PersonId
参数的是什么?根据我的理解陈述ds.Tables[0].Rows[1].Delete()
表示删除table[0]
的第二行,为什么它需要@PersonId
?
static void DataSetUpdateDelete()
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Server = GM\\SQLEXPRESS; Database = Reflection; Trusted_Connection = True;";
cn.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Person", cn);
//Create the update command
SqlCommand update = new SqlCommand();
update.Connection = cn;
update.CommandType = CommandType.Text;
update.CommandText = "UPDATE Person SET FirstName = @FirstName, LastName = @LastName WHERE PersonId = @PersonId";
//Create the parameters
update.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.VarChar, 50, "FirstName"));
update.Parameters.Add(new SqlParameter("@LastName", SqlDbType.VarChar, 50, "LastName"));
update.Parameters.Add(new SqlParameter("@PersonId", SqlDbType.Int, 0, "PersonId"));
//Create the delete command
SqlCommand delete = new SqlCommand();
delete.Connection = cn;
delete.CommandType = CommandType.Text;
delete.CommandText = "DELETE FROM Person WHERE PersonId = @PersonId";
//Create the parameters
SqlParameter deleteParameter = new SqlParameter("@PersonId", SqlDbType.Int, 0, "PersonId");
deleteParameter.SourceVersion = DataRowVersion.Original;
delete.Parameters.Add(deleteParameter);
//Associate the update and delete commands with the DataAdapter.
da.UpdateCommand = update;
da.DeleteCommand = delete;
//Get the data.
DataSet ds = new DataSet();
da.Fill(ds, "Person");
//Update the first row
ds.Tables[0].Rows[0]["FirstName"] = "Jack";
ds.Tables[0].Rows[0]["LastName"] = "Johnson";
//Delete the second row.
ds.Tables[0].Rows[1].Delete();
//Updat the database.
da.Update(ds.Tables[0]);
cn.Close();
}