我正在尝试更新 ID 值。因此,如果 ID 为 1,则新值为 2,则应将旧 ID 替换为新 ID。
后端:
public static void UpdateLocation( int locationID, SqlConnection connection, SqlTransaction transaction )
{
StringBuilder sqlString = new StringBuilder();
SqlCommand command;
sqlString.Append( "UPDATE [Location] SET " );
sqlString.Append( "description = @description " );
sqlString.Append( "WHERE locationID = @locationID " );
command = new SqlCommand( sqlString.ToString(), connection );
if( ( transaction != null ) ) command.Transaction = transaction;
command.Parameters.Add( "@locationID", SqlDbType.Int ).Value = locationID;
command.Parameters.Add( "@description", SqlDbType.VarChar ).Value = description;
int rowsAffected = command.ExecuteNonQuery();
if( !( rowsAffected == 1 ) )
{
throw new Exception( "An error has occurred while updating UpdateMedicationDispenseStatus." );
}
}
前端:
private void btnFromLocation_Click( object sender, System.Windows.RoutedEventArgs e )
{
if( !( ( Locations )cmbLocationDescriptionList.SelectedItem == ( Locations )cmbDescriptionListTo.SelectedItem ) )
{
for( int i = 0; i < lstMedicationForCurrentLocation.SelectedItems.Count; i++ )
{
location = (Locations)cmbDescriptionListTo.SelectedItem;
LocationData.UpdateLocation( location.LocationID );
编辑我已经向 SQL 查询添加了一个参数,但仍然没有更新